flat assembler
Message board for the users of flat assembler.

Index > Linux > section vs. segment notations

Author
Thread Post new topic Reply to topic
moveax41h



Joined: 18 Feb 2018
Posts: 59
moveax41h 24 Jul 2018, 16:47
One area of confusion for me since I started writing asm on Linux is there seem to be a couple of notations which can produce executables:

1. The programmer specifies sections - this produces a .o file which can be linked in with other code such as glibc.

2. The programmer specifies segments and can also specify in the asm file that it is to be executable, then fasm directly created an executable file without the .o file.

Can someone explain the use-cases for each of these methods (why I'd want to do one or the other) and also, I have a question - is it possible to use the segment style but still link in other code? Also, I noticed the permissions seem to work different on segments than sections.

I remember on Windows, I was always using the segment style but I had access to header files which allowed me to still be able to use external functions.

Note, I did see some info about this in the fasm manual, but I wasn't able to connect the dots with the info there as to why I'd want to do one of these methods over the other.[/b][/i]

_________________
-moveax41h
Post 24 Jul 2018, 16:47
View user's profile Send private message Reply with quote
Ali.Z



Joined: 08 Jan 2018
Posts: 727
Ali.Z 24 Jul 2018, 19:08
i do understand sections segments paging virtual memory.
but i cant explain them well enough, because i didnt go deep in these subjects.

sections are programmer defined, use section to specify this section of code is related to code segment. section data related to data segment in physical memory.
you can set permissions for each section, for example if you mark code section as writeable readable exectuable
then it will be treated as an executable data. (this is good for self modifying code)

segments are used to indecate each block of byte an their uses in ram, segments also have access rights, base address, and limit.
by default any executing code will go to CS, data in DS, stack SS etc.
each process have it own segment in memory, and according to intel each segment have privilege level.

paging also related to segments and the virtual memory.
by default and usually programs have 2 pages at least, 4kb for code and 4kb for data.
if you are storing more data in your program then another page with 4kb allocated for you.

sure paging and virtual memory can be OS dependent, OS can specifiy and modify processor mode via CR0 register. (note i said mode, because you cant enable PG bit without enabling PE bit in CR0)

also segments where mainly used in real-mode years ago. but now most of them are useless except GS FS the OS use them for it own purpose. (again it can be OS specific)

_________________
Asm For Wise Humans


Last edited by Ali.Z on 25 Jul 2018, 03:59; edited 1 time in total
Post 24 Jul 2018, 19:08
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8357
Location: Kraków, Poland
Tomasz Grysztar 24 Jul 2018, 20:39
This section/segment distinctions comes directly from the ELF specification.

When ELF is executable, segment definitions (in so-called Program Header Table) are required. When ELF is an object, the Section Table is required. But an ELF file may contain both tables at the same time. A single segment may then correspond to multiple sections, but not the other way around.

The segments define areas in memory with different attributes (like executable, writable, etc.) and are important for program loader. The sections are more important for the symbolic value, like their names, and they do not need to be present in the final executable. But the linker requires them, as it is usually the linker that constructs the Program Header Table and defines segments by combining sections with a similar attributes. After the segments are constructed, the section table can be safely discarded, though it usually isn't. But if you use fasm to produce ELF executable directly, it contains only Program Header Table and no section definitions at all. That's why you define segments directly then.
Post 24 Jul 2018, 20:39
View user's profile Send private message Visit poster's website Reply with quote
moveax41h



Joined: 18 Feb 2018
Posts: 59
moveax41h 24 Jul 2018, 23:37
Ahhh thank you for the great thorough explanation, I get it now!

I hadn't dug into the ELF format specs yet enough to know about the PHT... Though I find executable file formats fascinating and have published some research involving the Windows PE file format a while back. Fascinating stuff, I'm going to do some deep-diving on ELF format soon just to learn it like I did with PE.
Post 24 Jul 2018, 23:37
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8357
Location: Kraków, Poland
Tomasz Grysztar 25 Jul 2018, 09:40
I'm currently writing the first chapter of my new tutorial series, though I have started with PE. BTW, if you are interested in trivia about executable formats, please take a look at various off-shoots of that writing that I posted on Twitter.

And when I finish with PE (although this may take some time, there are many things to cover), I'm planning to start writing about ELF.
Post 25 Jul 2018, 09:40
View user's profile Send private message Visit poster's website Reply with quote
Gyver



Joined: 13 Jul 2009
Posts: 7
Gyver 26 Jul 2018, 09:25
See here if y want to understand the ELF format Wink


https://github.com/corkami/pics/tree/master/binary/elf101


Have a nice day!
Post 26 Jul 2018, 09:25
View user's profile Send private message Reply with quote
moveax41h



Joined: 18 Feb 2018
Posts: 59
moveax41h 26 Jul 2018, 11:38
That's a good one - thanks Gyver.

Tomasz I look forward to your tutorial.

I found that 010 Editor with the binary templates for EXE and ELF were helpful to me when working with PEs.
Post 26 Jul 2018, 11:38
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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.