flat assembler
Message board for the users of flat assembler.

Index > Linux > Section or Segment?

Author
Thread Post new topic Reply to topic
ChristianK



Joined: 28 Jun 2008
Posts: 4
ChristianK
Hi,
I'm new to linux assembly and have a problem with a simple binary file:

I looked at the 'elfexe' example of the FASM package and the code begins with 'segment readable executable'. I tried to disassemble the binary file with objdump, but it says that there are no sections in the file.

Using sections with NASM gives me the result i want. I can disassemble and analyse the file with objdump or readelf. When i use sections in FASM, i get only an .o file.

What is the difference between sections and segments and where are the advantages/disadvantages?
Post 28 Jun 2008, 09:36
View user's profile Send private message Reply with quote
AlexP



Joined: 14 Nov 2007
Posts: 561
Location: Out the window. Yes, that one.
AlexP
I too have been wondering about how the ELF file structure works (Don't worry Rev, I'm not asking for a link Razz).

ChristianK: You should google for the ELF File Structure
Post 29 Jun 2008, 05:01
View user's profile Send private message Visit poster's website Reply with quote
ChristianK



Joined: 28 Jun 2008
Posts: 4
ChristianK
Thanks for your answer.
I found many descriptions of the ELF file structure, but i can't understand why some executables have section and some have segments.
Since sections only appear in the linking view of an ELF file, all executables should have segments instead of section, or am i wrong?
Post 29 Jun 2008, 15:19
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
On executables sections are OPTIONAL and segments mandatory while in ELF objects sections are mandatory and segments not sure if optional or disallowed at all but I think it is the second.
Post 29 Jun 2008, 19:27
View user's profile Send private message Reply with quote
ChristianK



Joined: 28 Jun 2008
Posts: 4
ChristianK
Thank you, LocoDelAssembly. Smile
But there is one question left: C-Programs compiled with GCC have sections although they are executable. If there are sections and segments, do code and data exist twice in this file?
Post 30 Jun 2008, 13:55
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
Quote:

do code and data exist twice in this file?

No, only once but it is referenced by both tables.

Code:
readelf output for fasm generated executable:

Elf file type is EXEC (Executable file)
Entry point 0x8048074
There are 2 program headers, starting at offset 52

Program Headers:
  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
  LOAD           0x000074 0x08048074 0x08048074 0x0001f 0x0001f R E 0x1000
  LOAD           0x000093 0x08049093 0x08049093 0x0000d 0x0000d RW  0x1000
readelf output for ld generated executable (from a fasm generated 
object):

Elf file type is EXEC (Executable file)
Entry point 0x8048074
There are 2 program headers, starting at offset 52

Program Headers:
  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
  LOAD           0x000000 0x08048000 0x08048000 0x00093 0x00093 R E 0x1000
  LOAD           0x000094 0x08049094 0x08049094 0x00010 0x00010 RW  0x1000

 Section to Segment mapping:
  Segment Sections...
   00     .text 
   01     .bss 
ELF executable source:

; fasm demonstration of writing simple ELF executable

format ELF executable
entry start

segment readable executable

start:

    mov     eax,4
       mov     ebx,1
       mov     ecx,msg
     mov     edx,msg_size
        int     0x80

    mov     eax,1
       xor     ebx,ebx
     int     0x80

segment readable writeable

msg db 'Hello world!',0xA
msg_size = $-msg
ELF object source:

; fasm demonstration of writing simple ELF executable

format ELF 
public _start

section '.text' executable

_start:
     mov     eax,4
       mov     ebx,1
       mov     ecx,msg
     mov     edx,msg_size
        int     0x80

    mov     eax,1
       xor     ebx,ebx
     int     0x80

section  '.bss' writable

msg db 'Hello world!',0xA
msg_size = $-msg
    


Code:
#!/bin/sh

echo "readelf output for fasm generated executable:"
readelf exe -l

echo "readelf output for ld generated executable (from a fasm generated 
object):"
readelf obj -l

echo "ELF executable source:"
cat exe.asm

echo "ELF object source:"
cat obj.asm    


After passing strip to obj the size is reduced but still it is a little more than the double of exe's size generated with fasm. Well, part of that is the sections table but I don't remember now which other things are inside making obj bigger than exe but believe me that it is not because of code duplication Smile
Post 30 Jun 2008, 15:37
View user's profile Send private message Reply with quote
ChristianK



Joined: 28 Jun 2008
Posts: 4
ChristianK
Thank you very, very much LocoDelAssembly. I think i can understand the ELF format now. Very Happy
Post 30 Jun 2008, 18: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-2020, Tomasz Grysztar.

Powered by rwasa.