flat assembler
Message board for the users of flat assembler.

Index > Unix > [fasmg] Getting Mach-O section offset

Author
Thread Post new topic Reply to topic
adragomir



Joined: 04 Feb 2015
Posts: 2
Location: Romania
adragomir 04 Jun 2025, 12:56
I am trying to add a new style of `entry` to the current `macho.inc` format for Mach-O x64. The "LC_UNIXTHREAD" command is deprecated, and I am trying to replace it with a LC_MAIN command, but the problem is that I don't know how to get the parameter value for the entry point LC_MAIN command, which points to the start point in the format "file (__TEXT) offset of main()"

I have tried something like
Code:
macro entry2? regs&
  MachO.command LC_MAIN
    dq regs ; entryoff - this is wrong, I need the address from the start of __TEXT, and I don't know how to get it. 
    dq 0 ; initial stack size
  MachO.text
end macro
    


I have tried multiple ways of saving the section start in the file to a variable, of trying to load from it (load value from MachO:MachO.segment#1#_section#1.offset), but I guess I do not really understand the code in the macho.inc file.

Any help would be appreciated.
Post 04 Jun 2025, 12:56
View user's profile Send private message AIM Address Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8427
Location: Kraków, Poland
Tomasz Grysztar 04 Jun 2025, 13:35
Try this:
Code:
postpone
        repeat MachO.SEGMENT_NUMBER
                load SEGNAME from MachO:MachO.segment#%.segname
                if SEGNAME = '__TEXT'
                        load VMADDR from MachO:MachO.segment#%.vmaddr
                        __TEXT_VMADDR := VMADDR
                        break
                end if
        end repeat
end postpone    
It defines a __TEXT_VMADDR constant. Is that what you were looking for?
Post 04 Jun 2025, 13:35
View user's profile Send private message Visit poster's website Reply with quote
adragomir



Joined: 04 Feb 2015
Posts: 2
Location: Romania
adragomir 12 Jun 2025, 18:47
Yes ! This worked. I added this postpone statement somewhere and then I do something like

Code:
macro entry2? regs&
  MachO.command LC_MAIN
    dq regs - __TEXT_VMADDR ; entryoff
    dq 0 ; initial stack size
  MachO.text
end macro
    


Thank you so much !
Post 12 Jun 2025, 18:47
View user's profile Send private message AIM Address 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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.