flat assembler
Message board for the users of flat assembler.

Index > Main > Question about sections

Author
Thread Post new topic Reply to topic
upsurt



Joined: 14 Jan 2014
Posts: 51
upsurt
Hi

I've some questions about sections and best practices.

I read on a website that:

Quote:
The data section is used for declaring initialized data or constants. This data does not change at runtime. You can declare various constant values, file names or buffer size, etc., in this section.


and

Quote:
The bss section is used for declaring variables.


By now I used only the data section and defined there constants AND variables.

Is this bad and should I change that?

On another website they describe even more sections.
But I've also seen sections in code sample in the FASM board, that aren't defined there at all.

Code:
section 'database' data readable writeable    

...to define variables
Code:
section 'code' code executable readable    

...used instead of .text
Code:
section 'executbl' code executable readable    

...used instead of .text

And I've also seen variables and constants defined outside of any section at all.

Can anybody explain the best pratice and which sections someone should really use?
Post 12 Dec 2014, 10:03
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17450
Location: In your JS exploiting you and your system
revolution
There is no "best practice".

There is "standard" linker practice (where each linker does its own thing BTW) which is where most of the standard things come from. If you generate a linkable file (e.g. .OBJ file) then then the linker will probably expect certain things in certain places. It is up to you if you want to follow these usual practices.
Post 12 Dec 2014, 10:58
View user's profile Send private message Visit poster's website Reply with quote
upsurt



Joined: 14 Jan 2014
Posts: 51
upsurt
But I guess there a things you should take care off?
Like I would expect, you shouldn't mix up code and data definition inside the same section, or?
Post 12 Dec 2014, 12:31
View user's profile Send private message Reply with quote
evk1



Joined: 18 Jun 2014
Posts: 24
evk1
You can put code and data definitions in a single section if you want. Moreover, if your code and data fits in 4096 bytes, it should be more effective to use only one section. There is only one real reason to separate static data (code, strings, immutable tables, etc) from dynamic data. If your program contains non-writable sections, an OS can apply memory mapping mechanism to them, i.e. when your system has not enough memory, it takes away memory pages which contains some seldom accessed data from your program, and when your program tries to read such data, OS reallocates memory for it and reloads required data from your program file. For this reason executable file cannot be deleted or modified during the program execution. If a section has a write access then its actual content may differs from its original content and this way cannot be applied.

_________________
Sorry for my English
Post 12 Dec 2014, 20:46
View user's profile Send private message Reply with quote
evk1



Joined: 18 Jun 2014
Posts: 24
evk1
I think, uninitialized variables should be placed at the end of data section. I don't know internal structure of PE file format, but for example, in ELF in memory segments can occupy larger space then they occupies in files. Such segments contains all data from their images in the file, but also have some additional uninitialized space. For example:
Code:
segment readable writeable
var1 dd 123
var2 dd ?
var3 dd ?    

This segment will occupy only 4 bytes in file and will contain only var1 variable. var2 and var3 variables are located at the uninitialized part of segment, which occupies no file space. Such additional segment space can be added only to its end, so it is better to put uninitialized variables to the end of segment. I think PE have similar rules for sections.

This and my previous remark are related to PE programs and libraries. But then you are writing an object file you should place code, initialized data, uninitialized data, strings, etc at the different sections, because in such situations linker usually uses your file and some over object files to compose the correct program/library, and separating each kind of data helps it to find the best way to place all sections in the output file. However, I think, it is not necessary to use standard names for sections in this situation, but it is necessary to use standard section alignment and standard section access rights.

_________________
Sorry for my English
Post 12 Dec 2014, 23:19
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.