I finally found some decent documentation on the elf format (http://ftp.openwatcom.org/devel/docs/elf-64-gen.pdf), got almost all the expected output from readelf, but I still get corrupted text no matter what I try.
The output after running my program:
Code:
misty alpha
Creating test.elf
Writing Elf Header...
Writing Program Headers...
Writing Segment Headers...
Writing Symbols...
Writing Symbol Pool...
Writing Coded Area...
Flushing file...
./test.elf
Segmentation fault
readelf -all test.elf
ELF Header:
Magic: 7f 45 4c 46 02 01 01 ff 00 00 00 00 00 00 00 00
Class: ELF64
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: <unknown: ff>
ABI Version: 0
Type: EXEC (Executable file)
Machine: Advanced Micro Devices X86-64
Version: 0x1
Entry point address: 0x280
Start of program headers: 64 (bytes into file)
Start of section headers: 176 (bytes into file)
Flags: 0x0
Size of this header: 64 (bytes)
Size of program headers: 56 (bytes)
Number of program headers: 2
Size of section headers: 64 (bytes)
Number of section headers: 4
Section header string table index: 2
Section Headers:
[Nr] Name Type Address Offset
Size EntSize Flags Link Info Align
[ 0] <corrupt> NULL 0000000000000000 00000000
0000000000000000 0000000000000000 0 0 0
[ 1] > SYMTAB 00000000000001b0 00000000
0000000000000090 0000000000000018 A 2 6 0
[ 2] ^�ELF^B^A^A� STRTAB 0000000000000240 00000000
0000000000000040 0000000000000001 0 0 1
[ 3] PROGBITS 0000000000000280 00000000
0000000000000006 0000000000000001 WAXlp 0 0 0
Key to Flags:
W (write), A (alloc), X (execute), M (merge), S (strings), I (info),
L (link order), O (extra OS processing required), G (group), T (TLS),
C (compressed), x (unknown), o (OS specific), E (exclude),
l (large), p (processor specific)
There are no section groups in this file.
Program Headers:
Type Offset VirtAddr PhysAddr
FileSiz MemSiz Flags Align
NULL 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 0x0
LOAD 0x0000000000000000 0x0000000000000280 0x0000000000000280
0x0000000000000006 0x0000000000000006 RWE 0x0
Section to Segment mapping:
Segment Sections...
00
01
There is no dynamic section in this file.
There are no relocations in this file.
The decoding of unwind sections for machine type Advanced Micro Devices X86-64 is not currently supported.
Symbol table '>' contains 6 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 0000000000000000 0x1003e0002 FUNC LOCAL INTERNAL PRC[0xff01] <corrupt>
1: 0000000000000040 176 NOTYPE LOCAL DEFAULT UND <corrupt>
2: 0002000400400002 0 NOTYPE <unknown>: 4 DEFAULT bad section index[ 56] ^�ELF^B^A^A�
3: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND ^�ELF^B^A^A�
4: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND ^�ELF^B^A^A�
5: 0000000000000000 640 <unknown>: 7 LOCAL DEFAULT UND ELF^B^A^A�
No version information found in this file.
Exiting...
------------------
(program exited with code: 0)
Press return to continue
Code:
//#define name_is_pointer
#define name_from_pool
//#define name_from_top
//#define name_from_self
//#define open_wxhexeditor
d32 Named( segment_t *this, segment_t *segv, d16 n, d16 Tsize, char const *txt ) {
segment_t *cpool_seg = &segv[SEG_CPOOL];
shead64_t entry = {0};
u16 off = 0;
u16 pos = IndexOfSymbol(segv,txt,&off);
u16 abs = cpool_seg->pos + off;
d32 top = (abs > this->pos) ? abs - this->pos :
-((d32)(this->pos - abs));
if ( n < 0 || Tsize < 1 ) return 0;
#if defined(name_is_pointer)
return abs;
#elif defined(name_from_top)
return top;
#elif defined(name_from_self)
return top - (Tsize * n);
#elif defined(name_from_pool)
return off - 1;
#else
return pos;
#endif
}
void SetSegAndSym(
segment_t *this, segment_t *segv,
shead64_t *shent, named64_t *named,
u16 s, u16 n, char const *txt ) {
shent->sh_type = SHEAD_TYPE_NULL;
shent->sh_name = Named( &segv[SEG_SHEAD], segv, s, sizeof(shead64_t), txt );
shent->sh_addr = this->pos;
shent->sh_size = this->mem.size;
shent->sh_link = 0;
shent->sh_info = 0;
shent->sh_flags = 0;
shent->sh_offset = 0;
shent->sh_adralign = 0;
shent->sh_entsize = 1;
named->st_name = Named( &segv[SEG_NAMES], segv, n, sizeof(named64_t), txt );
named->st_value = this->pos;
named->st_size = this->mem.size;
named->st_info = NAMED_BIND_GLOBAL | NAMED_TYPE_NORM;
named->st_other = 0;
named->st_shndx = SYM_NORM;
}