flat assembler
Message board for the users of flat assembler.

Index > Main > [SOLVED] Avoiding redundant program headers

Author
Thread Post new topic Reply to topic
anbyte



Joined: 21 Jul 2024
Posts: 3
anbyte 21 Jul 2024, 12:07
Hi there, this is my first post on this forum.

I've been doing a small project in assembly, and I'm now in a situation where I want to split my source code across multiple files. Previously I've kept all my data in a single data segment, but now that I've split my code across multiple files, each file requires it's own code and data segment. This works, but it generates redundant prorgam headers in the ELF header of my executable, thus increasing the file size unnecessarily. I could put all of my data in a single data segment, but this would make splitting my code across multiple files seem redundant, as when you're reading a source file you can't see how the relevant data is being defined, what size it is, or what it even contains.

On to my question; how do people typically deal with this? Do they just let the program headers pile up? Do they have one file dedicated to a data segment and define all their initialized data there? Is there some preprocessor directives that could help me defer all my initialized data to a single data segment? Or is there something wrong in how I'm thinking about structuring my program?

Thanks in advance.


Last edited by anbyte on 21 Jul 2024, 18:59; edited 1 time in total
Post 21 Jul 2024, 12:07
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1792
Roman 21 Jul 2024, 13:48
Show your elf code.
Why elf code must be big when using couple files ?

Yes. Many programmers using several files.
Its include "some code or data"
This normally practices.
Post 21 Jul 2024, 13:48
View user's profile Send private message Reply with quote
anbyte



Joined: 21 Jul 2024
Posts: 3
anbyte 21 Jul 2024, 15:28
Roman wrote:
Show your elf code.
Why elf code must be big when using couple files ?

Yes. Many programmers using several files.
Its include "some code or data"
This normally practices.

To give an example, I may have a main.s and a print_buffer.s source files. In print_buffer.s I may have some reserved data.
Code:
PRINT_BUFFER_SIZE equ 4096

segment readable writeable
    print_buffer.idx rq 1
    print_buffer rb PRINT_BUFFER_SIZE

segment executable
print_buffer:
    ; ....
    ret

print_buffer_flush:
    ; ....
    ret    

Then, in my main.s I may have something along the lines of this:
Code:
format ELF64 executable

include "print_buffer.s"

segment readable
    msg_str db "hi there", 0x0a
    msg_len = $-msg_str

segment executable
entry start
start:
    mov rdi, msg_str
    mov rsi, msg_len
    call print_buffer

    call print_buffer_flush

    mov rax, 60  ; SYS_exit
    xor rdi, rdi
    syscall    

Well, the exact code doesn't matter that much (maybe I made an error but ignore it). I now have 2 segments for code, 1 for initialized data and 1 for reserved memory. If I needed some initialized data in print_buffer.s (perhaps a stride, doesn't really matter) I'd now have 2 data segments, so this is not even the best example. Point being, it's redundant to have 2 executable segments and 2 data segments if the routines could all be in the same segment, as well as the initialized data be in the same segment. I could split up my print_buffer.s file, but that means I'm either going to have to make some questionable assumptions (e.g. this source file HAS to be included in an executable segment), or the reader of the source code will lose important information, like how the data for the print buffer is defined when they are reading the print_buffer routine. I think the best solution would be if you could defer executable code, initialized data and reserved memory to 3 single segments and avoid redundancy in it's entirety, perhaps with some preprocessor directive. But maybe I'm doing something wrong in the structuring of this program?


Last edited by anbyte on 21 Jul 2024, 18:29; edited 1 time in total
Post 21 Jul 2024, 15:28
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20339
Location: In your JS exploiting you and your system
revolution 21 Jul 2024, 15:58
Options:

- You can use a normal (non executable) elf format in fasm, and use an external linker to combine each segment.

- There are also some small projects people have posted here that use macros to collect up code/data for each logical section and then use a final ".end", or similarly named macro, to make the final exe.

Here is a link to one example: https://board.flatassembler.net/topic.php?t=12012


Last edited by revolution on 21 Jul 2024, 19:01; edited 1 time in total
Post 21 Jul 2024, 15:58
View user's profile Send private message Visit poster's website Reply with quote
anbyte



Joined: 21 Jul 2024
Posts: 3
anbyte 21 Jul 2024, 18:58
revolution wrote:
Options:

- You can use a normal (non executable) elf format in fasm use an external linker to combine the each segment.

- There are also some small projects people have posted here that use macros to collect up code/data for each logical section and then use a final ".end", or similarly named macro, to make the final exe.

Here is a link to one example: https://board.flatassembler.net/topic.php?t=12012

I gave the linker a shot for producing regular ELF objects and linking them into an executable. I used a linker script to merge sections and reduce the binary file size as much as possible, but using the linker only seemed to introduce more unnecessary overhead. It's good to know if I decide to link against external libraries, but I'm not planning on with my current program. I guess I'll have to use macros to aggregate my code into single segments. The project that you referenced seems to be just what I'm looking for. Thank you for the help!
Post 21 Jul 2024, 18:58
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.