flat assembler
Message board for the users of flat assembler.

Index > Linux > Detecting program load address on Linux & Haiku x64

Author
Thread Post new topic Reply to topic
CandyMan



Joined: 04 Sep 2009
Posts: 413
Location: film "CandyMan" directed through Bernard Rose OR Candy Shop
CandyMan 25 Dec 2021, 00:35
How to detect the address where the program was loaded (its first byte address)? I am asking for a way for Linux and Haiku (assembler or pascal code is welcome).

_________________
smaller is better
Post 25 Dec 2021, 00:35
View user's profile Send private message Reply with quote
redsock



Joined: 09 Oct 2009
Posts: 430
Location: Australia
redsock 25 Dec 2021, 20:09
HeavyThing code:
Code:
include '../ht_defaults.inc'
include '../ht.inc'

public _start
falign
_start:
        call    ht$init

        mov     edi, .codeseg
        call    string$to_stdout

        mov     edi, ht$codeseg
        mov     esi, 16
        call    string$from_unsigned
        push    rax
        mov     rdi, rax
        call    string$to_stdoutln
        pop     rdi
        call    heap$free

        mov     edi, .loadaddr
        call    string$to_stdout

        mov     edi, _start
        mov     esi, 16
        call    string$from_unsigned
        push    rax
        mov     rdi, rax
        call    string$to_stdoutln
        pop     rdi
        call    heap$free


        mov     edi, .dsaddr
        call    string$to_stdout

        mov     rdi, ht$dataseg
        mov     esi, 16
        call    string$from_unsigned
        push    rax
        mov     rdi, rax
        call    string$to_stdoutln
        pop     rdi
        call    heap$free

        mov     eax, syscall_exit
        xor     edi, edi
        syscall
cleartext .codeseg, 'code segment is at: 0x'
cleartext .loadaddr, '_start is at: 0x'
cleartext .dsaddr, 'data segment is at: 0x'

include '../ht_data.inc'
    
Produces:
Code:
# ./test
code segment is at: 0x4000b0
_start is at: 0x401740
data segment is at: 0x4018e0
    
and this of course matches the ELF sections from thje binary:
Code:
# objdump -dx -M intel ./test |more

./test:     file format elf64-x86-64
./test
architecture: i386:x86-64, flags 0x00000112:
EXEC_P, HAS_SYMS, D_PAGED
start address 0x0000000000401740

Program Header:
    LOAD off    0x0000000000000000 vaddr 0x0000000000400000 paddr 0x0000000000400000 align 2**21
         filesz 0x00000000000018e0 memsz 0x00000000000018e0 flags r-x
    LOAD off    0x00000000000018e0 vaddr 0x00000000006018e0 paddr 0x00000000006018e0 align 2**21
         filesz 0x0000000000000160 memsz 0x0000000000000160 flags rw-

Sections:
Idx Name          Size      VMA               LMA               File off  Algn
  0 .text         00001830  00000000004000b0  00000000004000b0  000000b0  2**4
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
  1 .data         00000160  00000000006018e0  00000000006018e0  000018e0  2**4
                  CONTENTS, ALLOC, LOAD, DATA
    

Is this what you were after?

_________________
2 Ton Digital - https://2ton.com.au/
Post 25 Dec 2021, 20:09
View user's profile Send private message Reply with quote
ProMiNick



Joined: 24 Mar 2012
Posts: 798
Location: Russian Federation, Sochi
ProMiNick 25 Dec 2021, 20:56
Quote:
data segment is at: 0x4018e0

Quote:
# objdump -dx -M intel ./test |more
...
Code:
Program Header:
    ...
    LOAD off    0x00000000000018e0 vaddr 0x00000000006018e0 paddr 0x00000000006018e0 align 2**21
         ...

Sections:
Idx Name          Size      VMA               LMA               File off  Algn
  ...
  1 .data         00000160  00000000006018e0  00000000006018e0    


redsock, why solutions are think different about data segment/section
Post 25 Dec 2021, 20:56
View user's profile Send private message Send e-mail Reply with quote
redsock



Joined: 09 Oct 2009
Posts: 430
Location: Australia
redsock 25 Dec 2021, 21:09
ProMiNick wrote:
redsock, why solutions are think different about data segment/section
Excellent question, I should have looked closer at the outputs after I quickly threw the previous example together. As it turns out, this is because of the way I coded the symbol for ht$dataseg in https://2ton.com.au/library_as_html/ht_data.inc.html, where you can see that I declared the symbol right before the actual data section begins. I replaced the ht_data.inc file with:
Code:
        ; ------------------------------------------------------------------------
        ;       
        ; ht_data.inc: globals macro to define a global writeable data segment
        ;
        ; to use: globals {
        ;       someglobal dq 0
        ; }
        ; anywhere throughout your code, and it will all end up inside our single datasegment
        ;
        ; this is meant to be included _last_ (and our marker here defines the end of
        ; the code section)

ht$dataseg = $

        section '.data' writeable align 16
ht$actual_dataseg = $
        globalVars

    
and updated my test.asm:
Code:
include '../ht_defaults.inc'
include '../ht.inc'

globals
{
        _dsaddr dq      0
}

public _start
falign
_start:
        call    ht$init

        mov     edi, .codeseg
        call    string$to_stdout

        mov     edi, ht$codeseg
        mov     esi, 16
        call    string$from_unsigned
        push    rax
        mov     rdi, rax
        call    string$to_stdoutln
        pop     rdi
        call    heap$free

        mov     edi, .loadaddr
        call    string$to_stdout

        mov     edi, _start
        mov     esi, 16
        call    string$from_unsigned
        push    rax
        mov     rdi, rax
        call    string$to_stdoutln
        pop     rdi
        call    heap$free


        mov     edi, .dsaddr
        call    string$to_stdout

        mov     rdi, ht$dataseg
        mov     esi, 16
        call    string$from_unsigned
        push    rax
        mov     rdi, rax
        call    string$to_stdoutln
        pop     rdi
        call    heap$free

        mov     edi, .actualds
        call    string$to_stdout

        mov     rdi, ht$actual_dataseg
        mov     esi, 16
        call    string$from_unsigned
        push    rax
        mov     rdi, rax
        call    string$to_stdoutln
        pop     rdi
        call    heap$free

        mov     edi, .postht
        call    string$to_stdout

        mov     rdi, _dsaddr
        mov     esi, 16
        call    string$from_unsigned
        push    rax
        mov     rdi, rax
        call    string$to_stdoutln
        pop     rdi
        call    heap$free

        mov     eax, syscall_exit
        xor     edi, edi
        syscall
cleartext .codeseg, 'code segment is at: 0x'
cleartext .loadaddr, '_start is at: 0x'
cleartext .dsaddr, 'end of code segment is at: 0x'
cleartext .actualds, 'start of data segment is at: 0x'
cleartext .postht, 'after HeavyThing globals, our dataseg is at: 0x'

include '../ht_data.inc'
    
Which now produces:
Code:
# ./test
code segment is at: 0x4000b0
_start is at: 0x401740
end of code segment is at: 0x401ab4
start of data segment is at: 0x601ac0
after HeavyThing globals, our dataseg is at: 0x601c20
    
and that does match correctly the ELF binary inspection:
Code:
# objdump -dx -M intel ./test |more

./test:     file format elf64-x86-64
./test
architecture: i386:x86-64, flags 0x00000112:
EXEC_P, HAS_SYMS, D_PAGED
start address 0x0000000000401740

Program Header:
    LOAD off    0x0000000000000000 vaddr 0x0000000000400000 paddr 0x0000000000400000 align 2**21
         filesz 0x0000000000001ab4 memsz 0x0000000000001ab4 flags r-x
    LOAD off    0x0000000000001ac0 vaddr 0x0000000000601ac0 paddr 0x0000000000601ac0 align 2**21
         filesz 0x0000000000000168 memsz 0x0000000000000168 flags rw-

Sections:
Idx Name          Size      VMA               LMA               File off  Algn
  0 .text         00001a04  00000000004000b0  00000000004000b0  000000b0  2**4
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
  1 .data         00000168  0000000000601ac0  0000000000601ac0  00001ac0  2**4
                  CONTENTS, ALLOC, LOAD, DATA
    
Smile much better.

_________________
2 Ton Digital - https://2ton.com.au/
Post 25 Dec 2021, 21:09
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.