]> git.alrj.org Git - bold.git/blob - Bold/elf.py
065be520712b15deec3781cd590f139f5209539f
[bold.git] / Bold / elf.py
1 # -*- coding: utf-8 -*-
2
3 # kate: space-indent on; indent-width 2; mixedindent off; indent-mode python;
4
5 # Copyright (C) 2009 Amand 'alrj' Tihon <amand.tihon@alrj.org>
6 #
7 # This file is part of bold, the Byte Optimized Linker.
8 #
9 # You can redistribute this file and/or modify it under the terms of the
10 # GNU Lesser General Public License as published by the Free Software
11 # Foundation, version 2.1.
12
13
14 from BinArray import BinArray
15 from constants import *
16 from errors import *
17 import struct
18
19 # Helpful decorator
20 def nested_property(c):
21   return property(**c())
22
23 #--------------------------------------------------------------------------
24 #  Elf
25 #--------------------------------------------------------------------------
26
27 class Elf64(object):
28   """Handles an Elf64 object."""
29   interpreter = "/lib64/ld-linux-x86-64.so.2"
30
31   def __init__(self, path=None):
32     object.__init__(self)
33     self.header = Elf64_Ehdr()
34     self.header.owner = self
35     self.shdrs = []
36     self.phdrs = []
37     self.shlibs = []
38     self.sections = {}
39     self.segments = []
40     self.local_symbols = {}
41     self.global_symbols = {}
42     self.undefined_symbols = []
43
44     if path:
45       self.fromfile(path)
46
47   # Functions for relocatables files used as input
48
49   def fromfile(self, path):
50     f = file(path, "rb")
51
52     # Load Elf header
53     data = BinArray()
54     data.fromfile(f, Elf64_Ehdr.size)
55     self.header.fromBinArray(data)
56
57     # This linker only supports relocatable objects
58     if self.header.e_type != ET_REL:
59       raise NotRelocatableObject(path)
60
61     if self.header.e_ident.ei_class != ELFCLASS64:
62       raise UnsupportedObject(path, "Not %s" % ELFCLASS64)
63
64     if self.header.e_machine != EM_X86_64:
65       raise UnsupportedObject(path, "Not %s" % EM_X86_64)
66
67     # Load sections headers
68     f.seek(self.header.e_shoff)
69     for i in range(self.header.e_shnum):
70       data = BinArray()
71       data.fromfile(f, self.header.e_shentsize)
72       h = Elf64_Shdr(i, data)
73       h.owner = self
74       self.shdrs.append(h)
75
76     # Read sections content
77     for sh in self.shdrs:
78       data = BinArray()
79       if sh.sh_type != SHT_NOBITS:
80         f.seek(sh.sh_offset)
81         data.fromfile(f, sh.sh_size)
82       sh.content = data
83
84     f.close()
85
86   def resolve_names(self):
87     # The .shstrtab index is in Elf Header. find the sections names
88     strtab = self.shdrs[self.header.e_shstrndx].content
89
90     for sh in self.shdrs:
91       sh.name = strtab[int(sh.sh_name)]
92       self.sections[sh.name] = sh
93
94       # And resolve names in the section itself
95       sh.resolve_names()
96
97
98   def find_symbols(self):
99     for sh in self.shdrs:
100       if sh.sh_type == SHT_SYMTAB:
101         symtab = sh.content.symtab
102
103         for symbol in symtab:
104           if symbol.st_type == STT_FILE:
105             continue
106           if symbol.st_shndx == SHN_ABS:
107             continue
108           if symbol.st_shndx == SHN_UNDEF:
109             if symbol.name:
110               self.undefined_symbols.append(symbol.name)
111             continue
112
113           target_section = self.shdrs[symbol.st_shndx]
114
115           symbol_name = symbol.name
116           value = symbol.st_value
117           bind = symbol.st_binding
118
119           # We got a name, a target section, and an offset in the section
120           if symbol.st_binding == STB_LOCAL:
121             if symbol.st_type == STT_SECTION:
122               symbol_name = target_section.name
123             self.local_symbols[symbol_name] = (target_section, value)
124           else:
125             self.global_symbols[symbol_name] = (target_section, value)
126
127   def apply_relocation(self, all_global_symbols):
128     # find relocation tables
129     relocations = [sh for sh in self.shdrs if sh.sh_type in [SHT_REL, SHT_RELA]]
130     for sh in relocations:
131       target = sh.target.content
132
133       for reloc in sh.content.relatab:
134         
135         if reloc.symbol.st_shndx == SHN_UNDEF:
136           # This is an extern symbol, find it in all_global_symbols
137           sym_address = all_global_symbols[reloc.symbol.name]
138         else:
139           # source == in which section it is defined
140           source = self.shdrs[reloc.symbol.st_shndx].content
141           sym_address = source.virt_addr + reloc.symbol.st_value
142
143         target_ba = target.data # The actual BinArray that we'll modify
144         pc_address = target.virt_addr + reloc.r_offset
145
146         if reloc.r_type == R_X86_64_64:
147           format = "<Q" # Direct 64 bit address
148           target_value = sym_address + reloc.r_addend
149         elif reloc.r_type == R_X86_64_PC32:
150           format = "<i" # PC relative 32 bit signed
151           target_value = sym_address + reloc.r_addend - pc_address
152         elif reloc.r_type == R_X86_64_32:
153           format = "<I" # Direct 32 bit zero extended
154           target_value = sym_address + reloc.r_addend
155         elif reloc.r_type == R_X86_64_PC16:
156           format = "<h" # 16 bit sign extended pc relative
157           target_value = sym_address + reloc.r_addend - pc_address
158         elif reloc.r_type == R_X86_64_16:
159           format = "<H" # Direct 16 bit zero extended
160           target_value = sym_address + reloc.r_addend
161         elif reloc.r_type == R_X86_64_PC8:
162           format = "b" # 8 bit sign extended pc relative
163           target_value = sym_address + reloc.r_addend - pc_address
164         elif reloc.r_type == R_X86_64_8:
165           format = "b" # Direct 8 bit sign extended
166           target_value = sym_address + reloc.r_addend
167         else:
168           print "Unsupported relocation type: %s" % reloc.r_type
169           exit(1)
170
171         d = BinArray(struct.pack(format, target_value))
172         start = reloc.r_offset
173         end = start + len(d)
174         target_ba[start:end] = d
175
176
177   # Functions for executables files, as output
178
179   def add_phdr(self, phdr):
180     self.phdrs.append(phdr)
181     self.header.e_phnum = len(self.phdrs)
182     phdr.owner = self
183
184   def add_segment(self, segment):
185     self.segments.append(segment)
186
187   def layout(self, base_vaddr):
188     """Do the actual layout for final executable."""
189
190     virt_addr = base_vaddr
191     file_offset = 0
192     self.virt_addr = base_vaddr
193     self.file_offset = file_offset
194     for s in self.segments:
195         virt_addr += s.align
196         s.virt_addr = virt_addr
197         s.file_offset = file_offset
198         s.layout()
199         virt_addr += s.logical_size
200         file_offset += s.physical_size
201
202   def toBinArray(self):
203     ba = BinArray()
204     for s in self.segments:
205       ba.extend(s.toBinArray())
206     return ba
207
208
209 #--------------------------------------------------------------------------
210 #  Elf file header
211 #--------------------------------------------------------------------------
212
213 class Elf64_eident(object):
214   """Detailed representation for the Elf identifier."""
215   format = "16B"
216   size = struct.calcsize(format)
217   physical_size = size
218   logical_size = size
219
220   def __init__(self, rawdata=None):
221     object.__init__(self)
222     if rawdata is not None:
223       self.fromBinArray(rawdata)
224
225   def fromBinArray(self, rawdata):
226     t = struct.unpack(self.format, rawdata)
227     self.ei_magic = rawdata[:4]
228     self.ei_class = ElfClass(rawdata[4])
229     self.ei_data = ElfData(rawdata[5])
230     self.ei_version = ElfVersion(rawdata[6])
231     self.ei_osabi = ElfOsAbi(rawdata[7])
232     self.ei_abiversion = 0
233     self.ei_pad = [0, 0, 0, 0, 0, 0, 0]
234
235   def make_default_amd64(self):
236     self.ei_magic = BinArray([0x7f, 0x45, 0x4c, 0x46])
237     self.ei_class = ELFCLASS64
238     self.ei_data = ELFDATA2LSB
239     self.ei_version = EV_CURRENT
240     self.ei_osabi = ELFOSABI_SYSV
241     self.ei_abiversion = 0
242     self.ei_pad = [0, 0, 0, 0, 0, 0, 0]
243
244   def toBinArray(self):
245     ba = BinArray(self.ei_magic)
246     ba.append(self.ei_class)
247     ba.append(self.ei_data)
248     ba.append(self.ei_version)
249     ba.append(self.ei_osabi)
250     ba.append(self.ei_abiversion)
251     ba.extend(self.ei_pad)
252     return ba
253
254
255 class Elf64_Ehdr(object):
256   """Elf file header"""
257   format = "<16B 2H I 3Q I 6H"
258   size = struct.calcsize(format)
259   physical_size = size
260   logical_size = size
261   
262   def __init__(self, rawdata=None):
263     object.__init__(self)
264     self.e_ident = Elf64_eident()
265     self.e_type = ET_NONE
266     self.e_machine = EM_X86_64
267     self.e_version = EV_CURRENT
268     self.e_entry = 0
269     self.e_phoff = 0
270     self.e_shoff = 0
271     self.e_flags = 0
272     self.e_ehsize = self.size
273     self.e_phentsize = Elf64_Phdr.size
274     self.e_phnum = 0
275     self.e_shentsize = Elf64_Shdr.size
276     self.e_shnum = 0
277     self.e_shstrndx = 0
278     if rawdata is not None:
279       self.fromBinArray(rawdata)
280
281   def fromBinArray(self, rawdata):
282     t = struct.unpack(self.format, rawdata)
283     self.e_ident = Elf64_eident(BinArray(rawdata[:16]))
284     self.e_type = ElfType(t[16])
285     self.e_machine = ElfMachine(t[17])
286     self.e_version = ElfVersion(t[18])
287     self.e_entry = t[19]
288     self.e_phoff = t[20]
289     self.e_shoff = t[21]
290     self.e_flags = t[22]
291     self.e_ehsize = t[23]
292     self.e_phentsize = t[24]
293     self.e_phnum = t[25]
294     self.e_shentsize = t[26]
295     self.e_shnum = t[27]
296     self.e_shstrndx = t[28]
297
298   def toBinArray(self):
299     # Build a list from e_ident and all other fields, to feed struct.pack.
300     values = self.e_ident.toBinArray().tolist()
301     values.extend([self.e_type, self.e_machine, self.e_version, self.e_entry,
302       self.e_phoff, self.e_shoff, self.e_flags, self.e_ehsize, self.e_phentsize,
303       self.e_phnum, self.e_shentsize, self.e_shnum, self.e_shstrndx])
304     res = struct.pack(self.format, *values)
305     return BinArray(res)
306
307   def layout(self):
308     pass
309
310
311 #--------------------------------------------------------------------------
312 #  Elf Sections
313 #--------------------------------------------------------------------------
314
315 class Elf64_Shdr(object):
316   """Elf64 section header."""
317   format = "<2I 4Q 2I 2Q"
318   size = struct.calcsize(format)
319   physical_size = size
320   logical_size = size
321   
322   def __init__(self, index=None, rawdata=None):
323     object.__init__(self)
324     self.index = index
325     if rawdata is not None:
326       self.fromBinArray(rawdata)
327
328   def fromBinArray(self, rawdata):
329     t = struct.unpack(self.format, rawdata)
330     self.sh_name = t[0]
331     self.sh_type = ElfShType(t[1])
332     self.sh_flags = t[2]
333     self.sh_addr = t[3]
334     self.sh_offset = t[4]
335     self.sh_size = t[5]
336     self.sh_link = t[6]
337     self.sh_info = t[7]
338     self.sh_addralign = t[8]
339     self.sh_entsize = t[9]
340
341   def resolve_names(self):
342     self.content.resolve_names(self.owner)
343
344   @nested_property
345   def content():
346     def fget(self):
347       return self._content
348     def fset(self, data):
349       """Use the Section factory to get the subclass corresponding to the
350          session type specified in this header)."""
351       self._content = Section(self, data)
352     return locals()
353
354 # For sections that contain elements of specific types :
355
356 class Elf64_Sym(object):
357   """Symbol Table entry"""
358   format = "<I 2B H 2Q "
359   entsize = struct.calcsize(format)
360   def __init__(self, rawdata=None):
361     object.__init__(self)
362     if rawdata is not None:
363       self.fromBinArray(rawdata)
364
365   @nested_property
366   def st_binding():
367     def fget(self):
368       return ElfSymbolBinding((self.st_info >> 4) & 0x0f)
369     def fset(self, value):
370       self.st_info = (((value & 0x0f) << 4) | (self.st_info & 0x0f))
371     return locals()
372
373   @nested_property
374   def st_type():
375     def fget(self):
376        return ElfSymbolType(self.st_info & 0x0f)
377     def fset(self, value):
378       self.st_info = ((self.st_info & 0xf0) | (value & 0x0f))
379     return locals()
380
381   @nested_property
382   def st_visibility():
383     def fget(self):
384       return ElfSymbolVisibility(self.st_other & 0x03)
385     def fset(self, value):
386       self.st_other = ((self.st_other & 0xfc) | (value & 0x03))
387     return locals()
388
389   def fromBinArray(self, rawdata):
390     t = struct.unpack(self.format, rawdata)
391     self.st_name = t[0] # index in the strtab pointed by sh_link
392     self.st_info = t[1]
393     self.st_other = t[2]
394     self.st_shndx = ElfSectionIndex(t[3])
395     self.st_value = t[4]
396     self.st_size = t[5]
397
398
399 class Elf64_Rel(object):
400   format = "<2Q"
401   def __init__(self, rawdata=None):
402     object.__init__(self)
403     self.r_addend = 0 # No addend in a Rel.
404     if rawdata is not None:
405       self.fromBinArray(rawdata)
406
407   def fromBinArray(sef, rawdata):
408     t = struct.unpack(self.format, rawdata)
409     self.r_offset = t[0]
410     self.r_info = t[1]
411
412   @nested_property
413   def r_sym():
414     def fget(self):
415       return (self.r_info >> 32) & 0xffffffff
416     def fset(self, value):
417       self.r_info = ((value & 0xffffffff) << 32) | (self.r_info & 0xffffffff)
418     return locals()
419
420   @nested_property
421   def r_type():
422     def fget(self):
423       return Amd64Relocation(self.r_info & 0xffffffff)
424     def fset(self, value):
425       self.r_info = (self.r_info & 0xffffffff00000000) | (value & 0xffffffff)
426     return locals()
427
428
429 class Elf64_Rela(Elf64_Rel):
430   format = "<2Q q"
431   def __init__(self, rawdata=None):
432     Elf64_Rel.__init__(self, rawdata)
433
434   def fromBinArray(self, rawdata):
435     t = struct.unpack(self.format, rawdata)
436     self.r_offset = t[0]
437     self.r_info = t[1]
438     self.r_addend = t[2]
439
440
441 class Elf64_Dyn(object):
442   format = "<2Q"
443   size = struct.calcsize(format)
444   def __init__(self, tag, value):
445     object.__init__(self)
446     self.d_tag = tag
447     self.d_val = value
448
449   @nested_property
450   def d_ptr():
451     def fget(self):
452       return self.d_val
453     def fset(self, value):
454       self.d_val = value
455     return locals()
456
457   def toBinArray(self):
458     ba = BinArray()
459     ba.fromstring(struct.pack(self.format, self.d_tag, self.d_val))
460     return ba
461
462 # Sections types :
463
464 def Section(shdr, data=None):
465   """A section factory"""
466   dataclass = {
467     SHT_NULL:           SNull,
468     SHT_PROGBITS:       SProgBits,
469     SHT_SYMTAB:         SSymtab,
470     SHT_STRTAB:         SStrtab,
471     SHT_RELA:           SRela,
472     SHT_HASH:           SHash,
473     SHT_DYNAMIC:        SDynamic,
474     SHT_NOTE:           SNote,
475     SHT_NOBITS:         SNobits,
476     SHT_REL:            SRel,
477     SHT_SHLIB:          SShlib,
478     SHT_DYNSYM:         SDynsym
479   }
480   if shdr.sh_type in dataclass:
481     return dataclass[shdr.sh_type](shdr, data)
482   else:
483     return BaseSection(shdr, data)
484
485
486 class BaseSection(object):
487   def __init__(self, shdr, rawdata=None):
488     object.__init__(self)
489     self.data = None
490     self.header = shdr
491     if rawdata is not None:
492       self.fromBinArray(rawdata)
493
494   def fromBinArray(self, rawdata):
495     self.data = rawdata
496
497   def toBinArray(self):
498     if self.data:
499       return self.data
500     else:
501       return BinArray()
502
503   def resolve_names(self, elf):
504     """Nothing to resolve."""
505     pass
506
507   @nested_property
508   def size():
509     def fget(self):
510       return len(self.data)
511     return locals()
512   physical_size = size
513   logical_size = size
514
515   def layout(self):
516     pass
517
518
519 class SNull(BaseSection):
520   def __init__(self, shdr, data=None):
521     BaseSection.__init__(self, shdr, None)
522
523
524 class SProgBits(BaseSection):
525   def __init__(self, shdr, data=None):
526     BaseSection.__init__(self, shdr, data)
527
528
529 class SSymtab(BaseSection):
530   entsize = struct.calcsize(Elf64_Sym.format)
531   def __init__(self, shdr, data=None):
532     self.symtab = []
533     BaseSection.__init__(self, shdr, data)
534
535   def fromBinArray(self, data):
536     BaseSection.fromBinArray(self, data)
537     nument = len(data) / self.entsize
538     for i in range(nument):
539       start = i * self.entsize
540       end = i * self.entsize + self.entsize
541       self.symtab.append(Elf64_Sym(data[start:end]))
542
543   def resolve_names(self, elf):
544     # For a symtab, the strtab is indicated by sh_link
545     strtab = elf.shdrs[self.header.sh_link].content
546     # Resolve for all symbols in the table
547     for sym in self.symtab:
548       sym.name = strtab[sym.st_name]
549
550   def __getitem__(self, key):
551     return self.symtab[key]
552
553
554 class SStrtab(BaseSection):
555   """This one behaves in two completely different ways.
556   If it's given a section header and data, it will act as read-only, only to
557   be used for name resolution.
558   If it's not given any argument, it can be used to create a new Strtab."""
559   def __init__(self, shdr=None, data=None):
560     self.readonly = (shdr is not None)
561     self.strtab = {}
562     self.table = []
563     BaseSection.__init__(self, shdr, data)
564     self.virt_addr = None
565
566   def toBinArray(self):
567     if self.readonly:
568       return BaseSection.toBinArray()
569
570     ba = BinArray()
571     keys = self.strtab.keys()
572     keys.sort()
573     for k in keys:
574       ba.fromstring(self.strtab[k] + "\0")
575     return ba
576
577   @nested_property
578   def size():
579     def fget(self):
580       if self.readonly:
581         return len(data)
582       if len(self.strtab) == 0:
583         return 0
584       return sum((len(x)+1 for x in self.strtab.values()))
585     return locals()
586   physical_size = size
587   logical_size = size
588
589   def iteritems(self):
590     return self.strtab.iteritems()
591
592   # Resolution functions
593
594   def fromBinArray(self, data):
595     BaseSection.fromBinArray(self, data)
596     itab = data.tostring().split('\0')
597     i = 0
598     for sname in itab:
599       self.strtab[i] = sname
600       i += len(sname) + 1
601
602   def __getitem__(self, key):
603     if key in self.strtab:
604       return self.strtab[key]
605     else:
606       v = self.data[key:].tostring().split('\0')[0]
607       self.strtab[key] = v
608       return v
609
610   # Executable creation functions
611
612   def append(self, string):
613     if len(self.strtab) == 0:
614       offset = 0
615     else:
616       last = max(self.strtab.keys())
617       offset = last + len(self.strtab[last]) + 1 # for the \0
618     self.strtab[offset] = string
619     return offset
620
621   def layout(self):
622     pass
623
624
625 class SRela(BaseSection):
626   entsize = struct.calcsize(Elf64_Rela.format)
627   def __init__(self, shdr, data=None):
628     self.relatab = []
629     BaseSection.__init__(self, shdr, data)
630
631   def fromBinArray(self, data):
632     BaseSection.fromBinArray(self, data)
633     nument = len(data) / self.entsize
634     for i in range(nument):
635       start = i * self.entsize
636       end = i * self.entsize + self.entsize
637       self.relatab.append(Elf64_Rela(data[start:end]))
638
639   def resolve_names(self, elf):
640     """Badly named, this wil resolve to a symtab entry..."""
641     # sh_link leads to the symtab
642     self.symtab = elf.shdrs[self.header.sh_link].content
643     # sh_info links to the section on which the relocation applies
644     self.header.target = elf.shdrs[self.header.sh_info]
645     for r in self.relatab:
646       r.symbol = self.symtab[r.r_sym]
647
648
649
650 class SHash(BaseSection):
651   pass
652
653
654 class SDynamic(BaseSection):
655   pass
656
657
658 class SNote(BaseSection):
659   pass
660
661
662 class SNobits(BaseSection):
663   size = 0
664   physical_size = 0
665
666   @nested_property
667   def logical_size():
668     def fget(self):
669       return self.header.sh_size
670     return locals()
671
672   def toBinArray(self):
673     return BinArray()
674
675 class SRel(BaseSection):
676   pass
677
678
679 class SShlib(BaseSection):
680   pass
681
682
683 class SDynsym(SSymtab):
684   pass
685
686
687 class Elf64_Phdr(object):
688   format = "<2I 6Q"
689   size = struct.calcsize(format)
690   physical_size = size
691   logical_size = size
692
693   def __init__(self):
694     object.__init__(self)
695     self.p_type = PT_NULL
696     self.p_flags = PF_X + PF_W + PF_R
697     self.p_offset = 0
698     self.p_vaddr = 0
699     self.p_paddr = 0
700     self.p_filesz = 0
701     self.p_memsz = 0
702     self.p_align = 1
703
704   def toBinArray(self):
705     res = struct.pack(self.format, self.p_type, self.p_flags, self.p_offset,
706       self.p_vaddr, self.p_paddr, self.p_filesz, self.p_memsz, self.p_align)
707     return BinArray(res)
708
709   def layout(self):
710     pass
711
712   def update_from_content(self, content):
713     """ Update ofset, address and sizes.
714     After having applied layout(),the content knows all these values."""
715     self.p_offset = content.file_offset
716     self.p_vaddr = content.virt_addr
717     self.p_filesz = content.physical_size
718     self.p_memsz = content.logical_size
719
720
721 class BaseSegment(object):
722   def __init__(self, align=0):
723     object.__init__(self)
724     self.align = align
725     self.content = []
726
727   def add_content(self, content):
728     self.content.append(content)
729
730   def toBinArray(self):
731     ba = BinArray()
732     for c in self.content:
733       ba.extend(c.toBinArray())
734     return ba
735
736   @nested_property
737   def size():
738     def fget(self):
739       return sum(c.size for c in self.content)
740     return locals()
741   physical_size = size
742   logical_size = size
743
744
745 class TextSegment(BaseSegment):
746   def __init__(self, align=0):
747     BaseSegment.__init__(self, align)
748
749   def layout(self):
750     virt_addr = self.virt_addr
751     file_offset = self.file_offset
752     for i in self.content:
753       i.virt_addr = virt_addr
754       i.file_offset = file_offset
755       i.layout()
756       virt_addr += i.logical_size
757       file_offset += i.physical_size
758
759
760 class DataSegment(BaseSegment):
761   def __init__(self, align=0):
762     BaseSegment.__init__(self, align)
763     self.nobits = []
764
765   def add_nobits(self, content):
766     self.nobits.append(content)
767
768   def layout(self):
769     virt_addr = self.virt_addr
770     file_offset = self.file_offset
771     for i in self.content:
772       i.virt_addr = virt_addr
773       i.file_offset = file_offset
774       i.layout()
775       virt_addr += i.logical_size
776       file_offset += i.physical_size
777     for i in self.nobits:
778       i.virt_addr = virt_addr
779       i.file_offset = 0
780       i.layout()
781       virt_addr += i.logical_size
782
783   @nested_property
784   def logical_size():
785     def fget(self):
786       return self.physical_size + sum(c.logical_size for c in self.nobits)
787     return locals()
788
789
790 class Dynamic(object):
791   def __init__(self):
792     object.__init__(self)
793     self.dyntab = []
794     self.strtab = SStrtab()
795
796   @nested_property
797   def size():
798     def fget(self):
799       # End the table with a DT_NULL without associated value.
800       return (Elf64_Dyn.size * len(self.dyntab) + struct.calcsize("Q"))
801     return locals()
802   physical_size = size
803   logical_size = size
804
805   def add_shlib(self, shlib):
806     offset = self.strtab.append(shlib)
807     self.dyntab.append(Elf64_Dyn(DT_NEEDED, offset))
808
809   def add_symtab(self, vaddr):
810     self.dyntab.append(Elf64_Dyn(DT_SYMTAB, vaddr))
811
812   def add_debug(self):
813     self.dyntab.append(Elf64_Dyn(DT_DEBUG, 0))
814
815   def layout(self):
816     # Adjust the address of the strtab, if 
817     if self.strtab.virt_addr is None:
818       print "Ooops, strtab's address is not known yet. Aborting."
819       exit(1)
820     else:
821       self.dyntab.append(Elf64_Dyn(DT_STRTAB, self.strtab.virt_addr))
822
823   @nested_property
824   def dt_debug_address():
825     def fget(self):
826       for i, d in enumerate(self.dyntab):
827         if d.d_tag == DT_DEBUG:
828           return self.virt_addr + (i*d.size + (d.size/2))
829     return locals()
830
831
832   def toBinArray(self):
833     ba = BinArray()
834     for d in self.dyntab:
835       ba.extend(d.toBinArray())
836     null = struct.pack("<Q", DT_NULL)
837     ba.fromstring(null)
838     return ba
839
840
841 class Interpreter(object):
842   default_interpreter = "/lib64/ld-linux-x86-64.so.2"
843
844   def __init__(self, interpreter=None):
845     object.__init__(self)
846     if interpreter:
847       self.interpreter = interpreter
848     else:
849       self.interpreter = self.default_interpreter
850
851   @nested_property
852   def size():
853     def fget(self):
854       # Null terminated
855       return len(self.interpreter) + 1
856     return locals()
857   physical_size = size
858   logical_size = size
859
860   def toBinArray(self):
861     ba = BinArray(self.interpreter)
862     ba.append(0)
863     return ba
864
865   def layout(self):
866     pass
867