[Wisp-cvs] wisp/users/dig elf.py,1.11,1.12 makehello.py,1.10,1.11
Status: Alpha
Brought to you by:
digg
From: <di...@us...> - 2003-02-17 20:27:23
|
Update of /cvsroot/wisp/wisp/users/dig In directory sc8-pr-cvs1:/tmp/cvs-serv22412 Modified Files: elf.py makehello.py Log Message: first ELF32 executable with symbols (static linkage, no relocation) generated Index: elf.py =================================================================== RCS file: /cvsroot/wisp/wisp/users/dig/elf.py,v retrieving revision 1.11 retrieving revision 1.12 diff -u -d -r1.11 -r1.12 --- elf.py 16 Feb 2003 10:48:17 -0000 1.11 +++ elf.py 17 Feb 2003 20:27:18 -0000 1.12 @@ -370,7 +370,7 @@ t.align(4) t.emit_string('\0' * 40) # the null entry for name in names: - t.emit_tetra_sum(['.shstr/strings/' + name]) + t.emit_tetra_sum(['.shstrtab/strings/' + name]) t.emit_tetra_sum([name + '/sh_type']) t.emit_tetra_sum([name + '/sh_flags']) t.emit_tetra_sum([name]) Index: makehello.py =================================================================== RCS file: /cvsroot/wisp/wisp/users/dig/makehello.py,v retrieving revision 1.10 retrieving revision 1.11 diff -u -d -r1.10 -r1.11 --- makehello.py 17 Feb 2003 00:19:08 -0000 1.10 +++ makehello.py 17 Feb 2003 20:27:18 -0000 1.11 @@ -18,6 +18,8 @@ m.place_symbol(name) return m +sections = {} + code = Linkie('<') # ia32 code.place_symbol('_start') code.emit_byte(0xBA); code.emit_tetra(14) # mov edx, 14 @@ -29,9 +31,11 @@ code.emit_byte(0xB8); code.emit_tetra(1) # mov eax, 1 code.emit_byte(0xCD); code.emit_byte(0x80) # int 0x80 -data = Linkie('<') # ia32 -data.place_symbol('message') -data.emit_string('Hello, world!\n') +sections['.text'] = code; del code + +sections['.data'] = Linkie('<') # ia32 +sections['.data'].place_symbol('message') +sections['.data'].emit_string('Hello, world!\n') def infer_by_prefix (name, dict): for l in range(len(name), -1, -1): @@ -42,26 +46,32 @@ def guess_shtype (name): return infer_by_prefix(name, {'': SHT.PROGBITS, '.bss': SHT.NOBITS, + '.dynstr': SHT.STRTAB, '.note': SHT.NOTE, - '.shstr': SHT.STRTAB}) + '.shstrtab': SHT.STRTAB, + '.symstr': SHT.STRTAB, + '.symtab': SHT.SYMTAB, + }) def guess_ptype (name): return infer_by_prefix(name, {'': PT.LOAD, '.note': PT.NOTE}) def guess_flags (name): - return infer_by_prefix(name, {'': 'rw--', - '.bss': 'rw--', - '.comment': '----', - '.data': 'rw--', - '.debug': '----', - '.line': '----', - '.note': '----', - '.rodata': 'r---', - '.shstr': '---s', - '.text': 'r-x-'}) + return infer_by_prefix(name, {'': 'rw--', + '.bss': 'rw--', + '.comment': '----', + '.data': 'rw--', + '.debug': '----', + '.dynstr': 'a--s', + '.line': '----', + '.note': '----', + '.rodata': 'r---', + '.shstrtab': '---s', + '.symstr': 'a--s', + '.text': 'r-x-'}) def guess_shflags (name): flags = guess_flags(name) sh_flags = 0 - if 'r' in flags: sh_flags |= SHF.ALLOC + if 'r' in flags or 'a' in flags: sh_flags |= SHF.ALLOC if 'w' in flags: sh_flags |= SHF.ALLOC | SHF.WRITE if 'x' in flags: sh_flags |= SHF.ALLOC | SHF.EXECINSTR if 's' in flags: sh_flags |= SHF.STRINGS @@ -76,26 +86,39 @@ symbols = {} -shstr = Linkie('<') -shstr.emit_byte(0) -symbols['.shstr/strings/.text'] = shstr.filesz() -shstr.emit_string('.text\0') -symbols['.shstr/strings/.data'] = shstr.filesz() -shstr.emit_string('.data\0') -symbols['.shstr/strings/.shstr'] = shstr.filesz() -shstr.emit_string('.shstr\0') - hello = Linkie('<') memory_boundary = 0x8048000 # must be at page boundary hello.extend(make_ELF32_header('<')) symbols['elf/type'] = ET.EXEC symbols['elf/machine'] = EM.I386 symbols['elf/flags'] = 0 # no flags for ia386 -program_header_table = make_ELF32_phtable('<', ['.text', '.data']) + +shentnames = ['.text', '.data', '.symstr', '.symtab', '.shstrtab'] + +sections['.shstrtab'] = Linkie('<') +sections['.shstrtab'].emit_byte(0) + +sections['.symstr'] = Linkie('<') +sections['.symstr'].emit_byte(0) + +sections['.symtab'] = Linkie('<') +sections['.symtab'].align(4) +sections['.symtab'].emit_string('\0' * 16) + +phentnames = [] +for name in shentnames: + if guess_pflags(name) <> 0: + phentnames.append(name) + symbols['.shstrtab/strings/' + name] = sections['.shstrtab'].filesz() + sections['.shstrtab'].emit_string(name) + sections['.shstrtab'].emit_byte(0) + +program_header_table = make_ELF32_phtable('<', phentnames) symbols['elf/phoff'] = hello.extend(program_header_table) -symbols['elf/phnum'] = 2 +symbols['elf/phnum'] = len(phentnames) -for name, section in (('.text', code), ('.data', data), ('.shstr', shstr)): +for name in shentnames: + section = sections[name] p_flags = guess_pflags(name) alignment = section.get_alignment() symbols[name + '/sh_align'] = alignment @@ -136,19 +159,30 @@ if address <> None: for symbol, value in section.get_symbols(): symbols[symbol] = address + value - print '%s = %08x' % (symbol, address + value) for symbol, value in section.get_symbols(): hello.place_symbol(symbol, offset + value) # for dump to work nicely + sofs = sections['.symstr'].filesz() + symbols['.symstr/strings/' + symbol] = sofs + sections['.symstr'].emit_string(symbol) + sections['.symstr'].emit_byte(0) + sections['.symtab'].emit_tetra(sofs) + sections['.symtab'].emit_tetra_sum([name]) + sections['.symtab'].emit_tetra(0) + sections['.symtab'].emit_byte(STB.GLOBAL << 4 | STT.NOTYPE) + sections['.symtab'].emit_byte(0) + sections['.symtab'].emit_wyde(shentnames.index(name) + 1) + for loc, typ, arg in section.get_notes(): hello.notify_linker(offset + loc, typ, arg) memory_boundary += section.memsz() memory_boundary = (memory_boundary + 0xFFF) & ~0xFFF # create section header table -section_header_table = make_ELF32_shtable('<', ['.text', '.data', '.shstr']) +section_header_table = make_ELF32_shtable('<', shentnames) symbols['elf/shoff'] = hello.extend(section_header_table) -symbols['elf/shnum'] = 4 -symbols['elf/shstrndx'] = 3 +symbols['elf/shnum'] = len(shentnames) + 1 +symbols['elf/shstrndx'] = shentnames.index('.shstrtab') + 1 +symbols['.symtab/sh_link'] = shentnames.index('.symstr') + 1 hello.link(symbols) hello.dump() |