Thread: [Wisp-cvs] wisp/users/dig makehello.py,1.11,1.12
Status: Alpha
Brought to you by:
digg
From: <di...@us...> - 2003-02-23 11:05:18
|
Update of /cvsroot/wisp/wisp/users/dig In directory sc8-pr-cvs1:/tmp/cvs-serv10809 Modified Files: makehello.py Log Message: extracted |make_executable| from the root level source Index: makehello.py =================================================================== RCS file: /cvsroot/wisp/wisp/users/dig/makehello.py,v retrieving revision 1.11 retrieving revision 1.12 diff -u -d -r1.11 -r1.12 --- makehello.py 17 Feb 2003 20:27:18 -0000 1.11 +++ makehello.py 23 Feb 2003 11:05:15 -0000 1.12 @@ -18,8 +18,6 @@ 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 @@ -31,11 +29,9 @@ code.emit_byte(0xB8); code.emit_tetra(1) # mov eax, 1 code.emit_byte(0xCD); code.emit_byte(0x80) # int 0x80 -sections['.text'] = code; del code - -sections['.data'] = Linkie('<') # ia32 -sections['.data'].place_symbol('message') -sections['.data'].emit_string('Hello, world!\n') +data = Linkie('<') # ia32 +data.place_symbol('message') +data.emit_string('Hello, world!\n') def infer_by_prefix (name, dict): for l in range(len(name), -1, -1): @@ -84,106 +80,113 @@ if 'x' in flags: p_flags |= PF.X return p_flags -symbols = {} +def make_executable (text = None, data = None, memory_bottom = 0x08048000): + sections = {} + sections['.text'] = code + sections['.data'] = data -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 + symbols = {} + hello = Linkie('<') + memory_boundary = memory_bottom # 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 -shentnames = ['.text', '.data', '.symstr', '.symtab', '.shstrtab'] + shentnames = ['.text', '.data', '.symstr', '.symtab', '.shstrtab'] -sections['.shstrtab'] = Linkie('<') -sections['.shstrtab'].emit_byte(0) + sections['.shstrtab'] = Linkie('<') + sections['.shstrtab'].emit_byte(0) -sections['.symstr'] = Linkie('<') -sections['.symstr'].emit_byte(0) + sections['.symstr'] = Linkie('<') + sections['.symstr'].emit_byte(0) -sections['.symtab'] = Linkie('<') -sections['.symtab'].align(4) -sections['.symtab'].emit_string('\0' * 16) + 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) + 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'] = len(phentnames) + program_header_table = make_ELF32_phtable('<', phentnames) + symbols['elf/phoff'] = hello.extend(program_header_table) + symbols['elf/phnum'] = len(phentnames) -for name in shentnames: - section = sections[name] - p_flags = guess_pflags(name) - alignment = section.get_alignment() - symbols[name + '/sh_align'] = alignment + for name in shentnames: + section = sections[name] + p_flags = guess_pflags(name) + alignment = section.get_alignment() + symbols[name + '/sh_align'] = alignment - if p_flags <> 0: - symbols[name + '/p_align'] = 0x1000 - hello.align(min(alignment, 0x1000)) - else: - # No program header entry => not loaded - # thusly no larger alignment necessary. - hello.align(min(alignment, 4)) + if p_flags <> 0: + symbols[name + '/p_align'] = 0x1000 + hello.align(min(alignment, 0x1000)) + else: + # No program header entry => not loaded + # thusly no larger alignment necessary. + hello.align(min(alignment, 4)) - hello.place_symbol(name) - offset = hello.filesz() + hello.place_symbol(name) + offset = hello.filesz() - if p_flags <> 0: - address = memory_boundary | (offset & 0xFFF) - # take into account alignments larger than a page - address = (address + alignment - 1) & ~(alignment - 1) - symbols[name + '/p_flags'] = guess_pflags(name) - symbols[name + '/p_type'] = guess_ptype(name) - symbols[name] = address - else: - address = None - symbols[name] = 0 + if p_flags <> 0: + address = memory_boundary | (offset & 0xFFF) + # take into account alignments larger than a page + address = (address + alignment - 1) & ~(alignment - 1) + symbols[name + '/p_flags'] = guess_pflags(name) + symbols[name + '/p_type'] = guess_ptype(name) + symbols[name] = address + else: + address = None + symbols[name] = 0 - symbols[name + '/offset'] = offset - symbols[name + '/memsz'] = section.memsz() - symbols[name + '/filesz'] = section.filesz() - symbols[name + '/sh_size'] = section.filesz() - symbols[name + '/sh_type'] = guess_shtype(name) - symbols[name + '/sh_info'] = 0 - symbols[name + '/sh_link'] = SHN.UNDEF - symbols[name + '/sh_entsize'] = 0 - symbols[name + '/sh_flags'] = guess_shflags(name) + symbols[name + '/offset'] = offset + symbols[name + '/memsz'] = section.memsz() + symbols[name + '/filesz'] = section.filesz() + symbols[name + '/sh_size'] = section.filesz() + symbols[name + '/sh_type'] = guess_shtype(name) + symbols[name + '/sh_info'] = 0 + symbols[name + '/sh_link'] = SHN.UNDEF + symbols[name + '/sh_entsize'] = 0 + symbols[name + '/sh_flags'] = guess_shflags(name) - hello.from_array(section.get_file()) - if address <> None: + hello.from_array(section.get_file()) + if address <> None: + for symbol, value in section.get_symbols(): + symbols[symbol] = address + value for symbol, value in section.get_symbols(): - symbols[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) + 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 + 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('<', shentnames) -symbols['elf/shoff'] = hello.extend(section_header_table) -symbols['elf/shnum'] = len(shentnames) + 1 -symbols['elf/shstrndx'] = shentnames.index('.shstrtab') + 1 -symbols['.symtab/sh_link'] = shentnames.index('.symstr') + 1 -hello.link(symbols) + # create section header table + section_header_table = make_ELF32_shtable('<', shentnames) + symbols['elf/shoff'] = hello.extend(section_header_table) + symbols['elf/shnum'] = len(shentnames) + 1 + symbols['elf/shstrndx'] = shentnames.index('.shstrtab') + 1 + symbols['.symtab/sh_link'] = shentnames.index('.symstr') + 1 + hello.link(symbols) + return hello + +hello = make_executable(text = code, data = data) hello.dump() f = open('hello', 'w') |