Diff for /wikisrc/examples/elf_executables_for_powerpc.mdwn between versions 1.2 and 1.3

version 1.2, 2012/02/05 07:14:36 version 1.3, 2020/09/09 15:42:48
Line 15  _So far, this wiki page examines only th Line 15  _So far, this wiki page examines only th
   
 #   A very simple C program   #   A very simple C program 
   
 This program is only one C file, which contains only one main function, which calls [printf(3)](http://netbsd.gw.com/cgi-bin/man-cgi?printf+3+NetBSD-current) to print a single message, then returns 0 as the exit status.   This program is only one C file, which contains only one main function, which calls [[!template id=man name="printf" section="3"]] to print a single message, then returns 0 as the exit status. 
           
   
   
Line 29  This program is only one C file, which c Line 29  This program is only one C file, which c
     }      }
           
   
 The C compiler _gcc_ likes to use its knowledge of builtin functions to manipulate code. The version of gcc in NetBSD 4.0.1/macppc will simplify the printf statement to puts("Greeting, Earth!"); so the main function effectively calls [puts(3)](http://netbsd.gw.com/cgi-bin/man-cgi?puts+3+NetBSD-current) once and then returns 0.   The C compiler _gcc_ likes to use its knowledge of builtin functions to manipulate code. The version of gcc in NetBSD 4.0.1/macppc will simplify the printf statement to puts("Greeting, Earth!"); so the main function effectively calls [[!template id=man name="puts" section="3"]] once and then returns 0. 
   
   
 We can apply [gcc(1)](http://netbsd.gw.com/cgi-bin/man-cgi?gcc+1+NetBSD-current) in the usual way to compile this program. (With NetBSD, _cc_ or _gcc_ invokes the same command, so we use either name.) Then we can run our program:   We can apply [[!template id=man name="gcc" section="1"]] in the usual way to compile this program. (With NetBSD, _cc_ or _gcc_ invokes the same command, so we use either name.) Then we can run our program: 
           
   
 $ cc -o greetings greetings.c  $ cc -o greetings greetings.c
Line 77  The _.s_ assembly file and the _.o_ obje Line 77  The _.s_ assembly file and the _.o_ obje
   
 #   The program in PowerPC assembly language   #   The program in PowerPC assembly language 
   
 The manual page for [gcc(1)](http://netbsd.gw.com/cgi-bin/man-cgi?gcc+1+NetBSD-current) explains that we can use the _-S_ option to stop gcc with the assembly code. For PowerPC targets, gcc outputs register numbers by default; the _-mregnames_ option tells gcc to output register names instead. If you are learning assembly language, then _cc -mregnames -S_ is a good way to produce examples of assembly code.   The manual page for [[!template id=man name="gcc" section="1"]] explains that we can use the _-S_ option to stop gcc with the assembly code. For PowerPC targets, gcc outputs register numbers by default; the _-mregnames_ option tells gcc to output register names instead. If you are learning assembly language, then _cc -mregnames -S_ is a good way to produce examples of assembly code. 
   
   
 The command _cc -mregnames -S greetings.c_ produces the output file _greetings.s_ which contains the assembly version of our main function. (If you want _greeting.s_ to contain PowerPC assembly code, then you need to use compiler that targets PowerPC.) The assembly syntax allows for comments, assembler directives, instructions and labels.   The command _cc -mregnames -S greetings.c_ produces the output file _greetings.s_ which contains the assembly version of our main function. (If you want _greeting.s_ to contain PowerPC assembly code, then you need to use compiler that targets PowerPC.) The assembly syntax allows for comments, assembler directives, instructions and labels. 
Line 205  Here is a copy of _greeting.s_ (from the Line 205  Here is a copy of _greeting.s_ (from the
         .ident  "GCC: (GNU) 4.1.2 20061021 prerelease (NetBSD nb3 20061125)"          .ident  "GCC: (GNU) 4.1.2 20061021 prerelease (NetBSD nb3 20061125)"
           
   
 The above code is not a complete, standalone assembly program! It only contains a main function, for linking with the C runtime and the C library. It obeys the ELF and PowerPC conventions for the use of registers. (These conventions require the code to save r31 but not r9.) The _bl puts_ instruction is our evidence that the program calls [puts(3)](http://netbsd.gw.com/cgi-bin/man-cgi?puts+3+NetBSD-current) instead of [printf(3)](http://netbsd.gw.com/cgi-bin/man-cgi?printf+3+NetBSD-current).   The above code is not a complete, standalone assembly program! It only contains a main function, for linking with the C runtime and the C library. It obeys the ELF and PowerPC conventions for the use of registers. (These conventions require the code to save r31 but not r9.) The _bl puts_ instruction is our evidence that the program calls [[!template id=man name="puts" section="3"]] instead of [[!template id=man name="printf" section="3"]]. 
   
   
 The compiler did not optimize the above code. Some optimizations might be obvious! Consider the code that saves argc and argv to the stack. We would can use r1 instead of copying r1 to r11. Going further, we would can delete the code and never save argc and argv, because this main function never uses argc and argv!   The compiler did not optimize the above code. Some optimizations might be obvious! Consider the code that saves argc and argv to the stack. We would can use r1 instead of copying r1 to r11. Going further, we would can delete the code and never save argc and argv, because this main function never uses argc and argv! 
Line 278  Now that we have the assembly code, ther Line 278  Now that we have the assembly code, ther
   1. The first step is to run the assembler (as), which translates the assembly code to machine code, and stores the machine code in an ELF relocatable object.     1. The first step is to run the assembler (as), which translates the assembly code to machine code, and stores the machine code in an ELF relocatable object. 
   2. The second step is to run the linker (ld), which combines some ELF relocatables into one ELF executable.     2. The second step is to run the linker (ld), which combines some ELF relocatables into one ELF executable. 
   
 There are various tools that can examine ELF files. The command [nm(1)](http://netbsd.gw.com/cgi-bin/man-cgi?nm+1+NetBSD-current) lists the global symbols in an object file. The commands [objdump(1)](http://netbsd.gw.com/cgi-bin/man-cgi?objdump+1+NetBSD-current) and [readelf(1)](http://netbsd.gw.com/cgi-bin/man-cgi?readelf+1+NetBSD-current) show other information. These commands can examine both relocatables and executables. Though the executable is more interesting, the relocatable is simpler.   There are various tools that can examine ELF files. The command [[!template id=man name="nm" section="1"]] lists the global symbols in an object file. The commands [[!template id=man name="objdump" section="1"]] and [[!template id=man name="readelf" section="1"]] show other information. These commands can examine both relocatables and executables. Though the executable is more interesting, the relocatable is simpler. 
   
 To continue our example, we can run the assembler with _greetings.s_ to produce _greetings.o_. We use the optimized code in _greetings.s_ from _cc -O2 -mregnames -S greetings.c_, because it was shorter. We feed our file _greeting.s_ to /usr/bin/as with a simple command.   To continue our example, we can run the assembler with _greetings.s_ to produce _greetings.o_. We use the optimized code in _greetings.s_ from _cc -O2 -mregnames -S greetings.c_, because it was shorter. We feed our file _greeting.s_ to /usr/bin/as with a simple command. 
           
     $ as -o greetings.o greetings.s      $ as -o greetings.o greetings.s
           
   
 The output _greetings.o_ is a relocatable object file, and [file(1)](http://netbsd.gw.com/cgi-bin/man-cgi?file+1+NetBSD-current) confirms this.   The output _greetings.o_ is a relocatable object file, and [[!template id=man name="file" section="1"]] confirms this. 
           
 $ file greetings.o  $ file greetings.o
     greetings.o: ELF 32-bit MSB relocatable, PowerPC or cisco 4500, version 1 (SYSV)      greetings.o: ELF 32-bit MSB relocatable, PowerPC or cisco 4500, version 1 (SYSV)
Line 323  The source _greetings.s_ had assembler d Line 323  The source _greetings.s_ had assembler d
   
 This command verifies the presence of the _.text_ and _.rodata.str1.4_ sections. The _.text_ section begins at file offset 0x34 and has size 0x2c, in bytes. The _.rodata.str1.4_ section begins at file offset 0x60 and has size 0x14.   This command verifies the presence of the _.text_ and _.rodata.str1.4_ sections. The _.text_ section begins at file offset 0x34 and has size 0x2c, in bytes. The _.rodata.str1.4_ section begins at file offset 0x60 and has size 0x14. 
   
 Because the source _greetings.s_ does not have assembler directives for the _.data_ or _.bss_ or _.comment_ section, there must be another explanation for those three sections. The _.data_ and _.bss_ section has size 0x0. Perhaps for traditional reasons, the assembler puts these sections into every object file. Because the source _greeting.s_ never mentioned the _.data_ or _.bss_ section, nor allocated space in them, so the assembler output them as empty sections. (The [a.out(5)](http://netbsd.gw.com/cgi-bin/man-cgi?a.out+5+NetBSD-current) format always had text, data and bss segments. The [elf(5)](http://netbsd.gw.com/cgi-bin/man-cgi?elf+5+NetBSD-current) format distinguishes segments and sections, and also allows for arbitrary sections like _.rodata.str1.4_ and _.comment_.)   Because the source _greetings.s_ does not have assembler directives for the _.data_ or _.bss_ or _.comment_ section, there must be another explanation for those three sections. The _.data_ and _.bss_ section has size 0x0. Perhaps for traditional reasons, the assembler puts these sections into every object file. Because the source _greeting.s_ never mentioned the _.data_ or _.bss_ section, nor allocated space in them, so the assembler output them as empty sections. (The [[!template id=man name="a.out" section="5"]] format always had text, data and bss segments. The [[!template id=man name="elf" section="5"]] format distinguishes segments and sections, and also allows for arbitrary sections like _.rodata.str1.4_ and _.comment_.) 
   
   
 That leaves the mystery of the _.comment_ section. The _objdump_ command accepts _-j_ to select a section and _-s_ to show the contents, so _objdump -j .comment -s greetings.o_ dumps the 0x3c bytes in that section.   That leaves the mystery of the _.comment_ section. The _objdump_ command accepts _-j_ to select a section and _-s_ to show the contents, so _objdump -j .comment -s greetings.o_ dumps the 0x3c bytes in that section. 

Removed from v.1.2  
changed lines
  Added in v.1.3


CVSweb for NetBSD wikisrc <wikimaster@NetBSD.org> software: FreeBSD-CVSweb