Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sections in assembly language program

I am not clearly understanding how sections (declared using section directive) in an assembly program are handled by the assembler and the linker. Following are are some of the queries :

  • What is the use of user-defined sections (sections other than the standard .text, .data, .bss) ?

  • What if I put code in data section and data in code section? How does the assembler handle it ?

  • If no sections are declared in the program what will the assembler do?

  • If only user defined sections are declared(no text,data and bss) what will the assembler do?

I know I can write assembly programs to test if a program will work in each case but I want to get the concept clear.

like image 685
vjain27 Avatar asked Mar 21 '12 17:03

vjain27


1 Answers

Sections are no more than independent memory sequences. Each new byte of data is placed into the currently open "program section". It is quite convenient while writing a function to have the associated data quite close in the source code, even though when it is loaded into memory it might be megabytes or gigabytes distant.

User defined program sections work the same way as the standard sections, though you will usually have to provide additional information to the linker and other code post-processing tools so that they are loaded into memory in a sensible way.

You can place executable code in a data section and vice versa, and most assemblers will not even warn about it. Executing code from a data section might require a little trickery; the reverse is usually easy.

Assemblers handle sections typically by writing the equivalent into the object module in the same order as the source code, leaving the rearrangement of like sections together to the linker. Only the most simplistic assemblers don't provide this ability. The original MSDOS .COM file assembler comes to mind.

Different assemblers have different philosophies about coddling the programmer. The traditional tactic is to assume that an assembly language programmer knows what they are doing, and doing literally what is written except what is not understood. Other assemblers are more helpful (or a pain in the ass, depending on your point of view), and complain about misaligned multi-byte structures, data or code type mismatches, etc., etc.

Based on the "helpfulness" of an assembler, failure to initiate a program section results in default behaviour (usually an assumed code .psect), or refusal to assemble with a fatal error. Even the most structured assembler does not care if there is no text, data, or bss. In fact, there are useful object modules consisting only of symbol definition with no data (or code) bytes at all.

like image 158
wallyk Avatar answered Nov 08 '22 00:11

wallyk