Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split elf.inc, main asm files manage segments #45

Merged
merged 1 commit into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions blue.asm
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
format elf64 executable 3

include "elf.inc"

segment readable writeable

include "defs.inc"
include "elf_template.inc"

segment readable executable

include "elf.inc"
include "linux.inc"
include "code_buffer.inc"

Expand Down
109 changes: 0 additions & 109 deletions elf.inc
Original file line number Diff line number Diff line change
@@ -1,113 +1,4 @@

segment readable writeable

;
; adapted from https://kevinboone.me/elfdemo.html
;

elf_binary:
.base_address = 0x400000

elf_binary_headers:
elf_header:
db 0x7f, 0x45, 0x4c, 0x46 ; magic number
db 0x02 ; 64 bit
db 0x01 ; little endian
db 0x01 ; elf version
db 0x00 ; target abi
dq 0x00 ; target abi version + 7 bytes undefined
dw 0x02 ; executable binary
dw 0x3e ; amd64 architecture
dd 0x01 ; elf version
.start_address:
dq -0x01 ; start address
dq 0x40 ; offset to program header
.section_header_offset:
dq -0x01 ; offset to section header
dd 0x00 ; architecture flags
dw 0x40 ; size of header
dw 0x38 ; size of program header
dw 0x01 ; number of program headers
dw 0x40 ; size of section header
dw 0x03 ; number of section headers
dw 0x02 ; index of strtab section header

.length = $ - elf_header
assert .length = 0x40

program_header:
dd 0x01 ; entry type: loadable segment
dd 0x05 ; segment flags: RX
dq 0x00 ; offset within file
dq elf_binary.base_address ; load position in virtual memory
dq elf_binary.base_address ; load position in physical memory
.sizes:
.size_in_file:
dq -0x01 ; size of the loaded section (file)
.size_in_memory:
dq -0x01 ; size of the loaded section (memory)
dq 0x200000 ; alignment boundary for sections

.length = $ - program_header
assert .length = 0x38

shstrtab:
db ".shstrtab"
db 0x00
db ".text"
db 0x00

.length = $ - shstrtab
assert .length = 0x10

elf_binary_section_headers:
section_header_0:
dq 0x00, 0x00, 0x00, 0x00 ; 64 bytes of 0s
dq 0x00, 0x00, 0x00, 0x00

.length = $ - section_header_0
assert .length = 0x40

program_code_section_header:
dd 0x0a ; offset to name in shstrtab
dd 0x01 ; type: program data
dq 0x06 ; flags - executable | in memory
.address:
dq -0x01 ; addr in virtual memory of section
.offset:
dq -0x01 ; offset in the file of this section
.size:
dq -0x01 ; size of this section in the file
dq 0x00 ; sh_link - not used
dq 0x01 ; alignment code (default??)
dq 0x00 ; sh_entsize - not used

.length = $ - program_code_section_header
assert .length = 0x40

shstrtab_section_header:
dd 0x00 ; offset to name in shstrtab
dd 0x03 ; type: string table
dq 0x00 ; flags - none
dq 0x00 ; addr in virtual memory of section - not used
.offset:
dq -0x01 ; offset in the file of this section
.size:
dq -0x01 ; size of this section in the file
dq 0x00 ; sh_link - not used
dq 0x01 ; alignment code (default??)
dq 0x00 ; sh_entsize - not used

.length = $ - shstrtab_section_header
assert .length = 0x40

elf_binary_wrapper_length = $ - elf_binary
elf_binary_headers_length = elf_header.length + program_header.length
elf_binary_section_headers_length = $ - elf_binary_section_headers
elf_binary_section_headers_offset = elf_binary_section_headers - elf_binary

segment readable executable

;
; expects
; - program code entry offset in eax
Expand Down
105 changes: 105 additions & 0 deletions elf_template.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@

;
; adapted from https://kevinboone.me/elfdemo.html
;

elf_binary:
.base_address = 0x400000

elf_binary_headers:
elf_header:
db 0x7f, 0x45, 0x4c, 0x46 ; magic number
db 0x02 ; 64 bit
db 0x01 ; little endian
db 0x01 ; elf version
db 0x00 ; target abi
dq 0x00 ; target abi version + 7 bytes undefined
dw 0x02 ; executable binary
dw 0x3e ; amd64 architecture
dd 0x01 ; elf version
.start_address:
dq -0x01 ; start address
dq 0x40 ; offset to program header
.section_header_offset:
dq -0x01 ; offset to section header
dd 0x00 ; architecture flags
dw 0x40 ; size of header
dw 0x38 ; size of program header
dw 0x01 ; number of program headers
dw 0x40 ; size of section header
dw 0x03 ; number of section headers
dw 0x02 ; index of strtab section header

.length = $ - elf_header
assert .length = 0x40

program_header:
dd 0x01 ; entry type: loadable segment
dd 0x05 ; segment flags: RX
dq 0x00 ; offset within file
dq elf_binary.base_address ; load position in virtual memory
dq elf_binary.base_address ; load position in physical memory
.sizes:
.size_in_file:
dq -0x01 ; size of the loaded section (file)
.size_in_memory:
dq -0x01 ; size of the loaded section (memory)
dq 0x200000 ; alignment boundary for sections

.length = $ - program_header
assert .length = 0x38

shstrtab:
db ".shstrtab"
db 0x00
db ".text"
db 0x00

.length = $ - shstrtab
assert .length = 0x10

elf_binary_section_headers:
section_header_0:
dq 0x00, 0x00, 0x00, 0x00 ; 64 bytes of 0s
dq 0x00, 0x00, 0x00, 0x00

.length = $ - section_header_0
assert .length = 0x40

program_code_section_header:
dd 0x0a ; offset to name in shstrtab
dd 0x01 ; type: program data
dq 0x06 ; flags - executable | in memory
.address:
dq -0x01 ; addr in virtual memory of section
.offset:
dq -0x01 ; offset in the file of this section
.size:
dq -0x01 ; size of this section in the file
dq 0x00 ; sh_link - not used
dq 0x01 ; alignment code (default??)
dq 0x00 ; sh_entsize - not used

.length = $ - program_code_section_header
assert .length = 0x40

shstrtab_section_header:
dd 0x00 ; offset to name in shstrtab
dd 0x03 ; type: string table
dq 0x00 ; flags - none
dq 0x00 ; addr in virtual memory of section - not used
.offset:
dq -0x01 ; offset in the file of this section
.size:
dq -0x01 ; size of this section in the file
dq 0x00 ; sh_link - not used
dq 0x01 ; alignment code (default??)
dq 0x00 ; sh_entsize - not used

.length = $ - shstrtab_section_header
assert .length = 0x40

elf_binary_wrapper_length = $ - elf_binary
elf_binary_headers_length = elf_header.length + program_header.length
elf_binary_section_headers_length = $ - elf_binary_section_headers
elf_binary_section_headers_offset = elf_binary_section_headers - elf_binary
5 changes: 4 additions & 1 deletion elf_test.asm
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
format elf64 executable 3

include "elf.inc"
segment readable writeable

include "elf_template.inc"

segment readable executable

include "elf.inc"
include "linux.inc"

program_code:
Expand Down
10 changes: 6 additions & 4 deletions elf_test_hello_world.asm
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
format elf64 executable 3

include "linux.inc"
include "elf.inc"

segment readable writeable

include "elf_template.inc"

fstat_buffer:
rb 48
.file_size:
rq 1
rb 88

segment readable executable

program_code:
.entry_offset = $ - program_code
db 0x48, 0xc7, 0xc0 ; mov rax, 1 - sys_write
Expand All @@ -32,7 +33,8 @@ program_code:

.length = $ - program_code

segment readable executable
include "elf.inc"
include "linux.inc"

expected_output_size = elf_binary_wrapper_length + program_code.length

Expand Down
Loading