Skip to content

Instantly share code, notes, and snippets.

@dockimbel
Created March 13, 2011 19:08
Show Gist options
  • Save dockimbel/868338 to your computer and use it in GitHub Desktop.
Save dockimbel/868338 to your computer and use it in GitHub Desktop.
REBOL [
Title: "Red/System ELF format emitter"
Author: "Nenad Rakocevic"
File: %ELF.r
Rights: "Copyright (C) 2011 Nenad Rakocevic. All rights reserved."
License: "BSD-3 - https://github.com/dockimbel/Red/blob/master/BSD-3-License.txt"
]
context [
defs: [
extensions [
exe %""
obj %.o
lib %.a
dll %.so
]
class [
none 0
c-32-bit 1
c-64-bit 2
]
encoding [
none 0
LSB 1 ;-- little endian data encoding
MSB 2 ;-- big endian data encoding
]
file-type [
none 0 ;-- no file type
relocatable 1 ;-- relocatable file
executable 2 ;-- executable file
shared 3 ;-- shared object file
core 4 ;-- core file
lo-proc #{FF00} ;-- processor-specific
hi-proc #{FFFF} ;-- processor-specific
]
machine [
none 0 ;-- no machine
m32 1 ;-- AT&T WE 32100
sparc 2 ;-- SPARC
i386 3 ;-- Intel 80386
m68k 4 ;-- Motorola 68000
m88k 5 ;-- Motorola 88000
i860 7 ;-- Intel 80860
mips 8 ;-- MIPS RS3000
]
s-type [
null 0 ;-- mark inactive section header
prog-bits 1 ;-- flags
sym-tab 2 ;-- symbol table (for link editing)
str-tab 3 ;-- string table
rela 4 ;-- relocations with addends
hash 5 ;-- symbol hash table
dynamic 6 ;-- dynamic linking
note 7 ;-- notes/comments
no-bits 8 ;-- ??
rel 9 ;-- relocations without addends
shlib 10 ;-- reserved (unused)
dyn-sym 11 ;-- symbol table (dynamic linking)
lo-proc #{70000000} ;-- reserved (proc-specific)
hi-proc #{7FFFFFFF} ;-- reserved (proc-specific)
lo-user #{80000000} ;-- reserved (lower bound reserved indexes)
hi-user #{8FFFFFFF} ;-- reserved (upper bound reserved indexes)
]
s-flags [
write 1 ;-- writable data
alloc 2 ;-- requires memory during program execution
exec 4 ;-- executable code
mask-proc #{F0000000} ;-- proc-specific semantics
]
]
memory-align: 4096 ;-- system page size
;file-align: 512 ;-- better keep it < memory-align
section-header-32: make struct! [
sh_name [integer!]
sh_type [integer!]
sh_flags [integer!]
sh_addr [integer!]
sh_offset [integer!]
sh_size [integer!]
sh_link [integer!]
sh_info [integer!]
sh_addralign [integer!]
sh_entsize [integer!]
] none
header-32: make struct! [
ei_mag0 [char] ;-- ma0-3: magic number
ei_mag1 [char]
ei_mag3 [char]
ei_mag3 [char]
ei_class [char]
ei_data [char]
ei_version [char]
ei_pad1 [char]
ei_pad2 [decimal!] ;-- 8-bytes padding
e_type [short]
e_machine [short]
e_version [integer!]
e_entry [integer!]
e_phoff [integer!]
e_shoff [integer!]
e_flags [integer!]
e_ehsize [short]
e_phentsize [short]
e_phnum [short]
e_shentsize [short]
e_shnum [short]
e_shstrndx [short]
] none
build-section-header: func [job [object!] type specs][
]
build-header: func [job [object!]][
fh: make struct! header-32 none
fh/ei_mag0: #"^(7F)"
fh/ei_mag0: #"E"
fh/ei_mag0: #"L"
fh/ei_mag0: #"F"
fh/e_class: defs/class/c-32-bit
fh/e_data: defs/encoding/LSB ;-- TBD: make it target-dependent
fh/e_version: 1 ;-- 0: invalid, 1: current
fh/machine: defs/machine/i386
;...
append job/buffer third fh
]
build: func [job [object!]][
]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment