S3c2440 code relocation details 3---analysis of link script

Publisher:HuanleLatest update time:2021-09-09 Source: eefocusKeywords:S3c2440 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

insert image description here

Linker script syntax


SECTIONS {

...

secname start BLOCK(align) (NOLOAD) : AT ( ldadr )

  { contents } >region :phdr =fill

...

}


explain:


 secname : section name

 start: starting address: runtime address (runtime addr); relocation address (relocate addr)

 AT (ldadr): optional (load addr: load address) If not written, LoadAddr = runtime addr

 Contents of { contents }: 

 start.o //The content is start.o file

 * (.text) All code snippet files

 start.o * (.text) files


elf file format


1 Link to get the elf file, which contains address information (load addr)


2 Using the loader


:: 2.1 For bare boards, it is the JTAG debugging tool


:: 2.2 For APP, the loader is also APP to parse the elf file and read it into the loading address of the memory


3 Run the program


4 If loadaddr != runtimeaddr the program itself needs to be relocated


The core program should be located at runtimeaddr (relocation addr) or link address when it is running


bin file


1 elf generates bin file 


2 Hardware mechanism startup


3 If the location of the bin file is not equal to runtimeaddr, the program itself implements relocation

insert image description here

The bin file/elf file does not save the bss segment. These are global variables with initial values ​​of 0 or not initialized.


When the program is running, the space corresponding to the bss segment is cleared


As an experiment, print out the global variable g_A in hexadecimal


/* 0xABCDEF12 */

void printHex(unsigned int val)

{

    int i;

    unsigned char arr[8];


    /* First take out the value of each bit*/

    for (i = 0; i < 8; i++)

    {

        arr[i] = val & 0xf;

        val >>= 4;   /* arr[0] = 2, arr[1] = 1, arr[2] = 0xF */

    }


    /* Print*/

    puts("0x");

    for (i = 7; i >=0; i--)

    {

        if (arr[i] >= 0 && arr[i] <= 9)

            putchar(arr[i] + '0');

        else if(arr[i] >= 0xA && arr[i] <= 0xF)

            putchar(arr[i] - 0xA + 'A');

    }

}


//Print the variable with initial value 0

int g_A = 0;

int g_B;


int main(void)

{

    uart0_init();


    puts("nrg_A = ");

    printHex(g_A);

    puts("nr");


In the above code, the bss segment g_A is equal to an inexplicable value and is not equal to 0, so the bss segment needs to be cleaned up.


Modify the lds link file


SECTIONS {

   .text   0  : { *(.text) }

   .rodata : { *(.rodata) }

   .data 0x30000000 : AT(0x700) 

   { 

      data_load_addr = LOADADDR(.data);

      data_start = . ;

      *(.data) 

      data_end = . ;

   }


   bss_start = .; //bss start address is the current position

   .bss  : { *(.bss) *(.COMMON) }

   bss_end = .; //bss end address is also the current position

}


Modify start.s and clear the bss segment


/* Clear BSS segment */

ldr r1, =bss_start

ldr r2, =bss_end

move r3, #0

clean:

    strb r3, [r1]

    add r1, r1, #1

    cmp r1, r2

    bne clean


    bl main

halt:


The current code global variable is 0. With just a few lines of code, you can save dozens or even thousands of global variables storage space.

Keywords:S3c2440 Reference address:S3c2440 code relocation details 3---analysis of link script

Previous article:S3c2440 code relocation details 2---Introduction and simple test of link script
Next article:S3c2440 code relocation details 4 --- copy code and link script improvements

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号