parameters: (self, elf_data, symbol_name, replacement)
return_statement: return replaced
docstring: The Linux/FreeBSD implementation of _replace_symbol.
docstring_summary: The Linux/FreeBSD implementation of _replace_symbol.
docstring_tokens: The Linux / FreeBSD implementation of _replace_symbol .
function: def _replace_symbol_elf(self, elf_data, symbol_name, replacement):
          """ The Linux/FreeBSD implementation of _replace_symbol. """
  
          replaced = False
  
          # Make sure we read in the correct endianness and integer size
          endian = "<>"[ord(elf_data[5:6]) - 1]
          is_64bit = ord(elf_data[4:5]) - 1 # 0 = 32-bits, 1 = 64-bits
          header_struct = endian + ("HHIIIIIHHHHHH", "HHIQQQIHHHHHH")[is_64bit]
          section_struct = endian + ("4xI4xIIII8xI", "4xI8xQQQI12xQ")[is_64bit]
          symbol_struct = endian + ("IIIBBH", "IBBHQQ")[is_64bit]
  
          header_size = struct.calcsize(header_struct)
          type, machine, version, entry, phoff, shoff, flags, ehsize, phentsize, phnum, shentsize, shnum, shstrndx \
            = struct.unpack_from(header_struct, elf_data, 16)
          section_offsets = []
          symbol_tables = []
          string_tables = {}
  
          # Seek to the section header table and find the symbol tables.
          ptr = shoff
          for i in range(shnum):
              type, addr, offset, size, link, entsize = struct.unpack_from(section_struct, elf_data[ptr:ptr+shentsize])
              ptr += shentsize
              section_offsets.append(offset - addr)
              if type == 0x0B and link != 0: # SHT_DYNSYM, links to string table
                  symbol_tables.append((offset, size, link, entsize))
                  string_tables[link] = None
  
          # Read the relevant string tables.
          for idx in list(string_tables.keys()):
              ptr = shoff + idx * shentsize
              type, addr, offset, size, link, entsize = struct.unpack_from(section_struct, elf_data[ptr:ptr+shentsize])
              if type == 3:
                  string_tables[idx] = elf_data[offset:offset+size]
  
          # Loop through to find the offset of the "blobinfo" symbol.
          for offset, size, link, entsize in symbol_tables:
              entries = size // entsize
              for i in range(entries):
                  ptr = offset + i * entsize
                  fields = struct.unpack_from(symbol_struct, elf_data[ptr:ptr+entsize])
                  if is_64bit:
                      name, info, other, shndx, value, size = fields
                  else:
                      name, value, size, info, other, shndx = fields
  
                  if not name:
                      continue
  
                  name = string_tables[link][name : string_tables[link].find(b'\0', name)]
                  if name == symbol_name:
                      if shndx == 0: # SHN_UNDEF
                          continue
                      elif shndx >= 0xff00 and shndx <= 0xffff:
                          assert False
                      else:
                          # Got it.  Make the replacement.
                          off = section_offsets[shndx] + value
                          elf_data[off:off+len(replacement)] = replacement
                          replaced = True
  
          return replaced
function_tokens: def _replace_symbol_elf ( self , elf_data , symbol_name , replacement ) : replaced = False # Make sure we read in the correct endianness and integer size endian = "<>" [ ord ( elf_data [ 5 : 6 ] ) - 1 ] is_64bit = ord ( elf_data [ 4 : 5 ] ) - 1 # 0 = 32-bits, 1 = 64-bits header_struct = endian + ( "HHIIIIIHHHHHH" , "HHIQQQIHHHHHH" ) [ is_64bit ] section_struct = endian + ( "4xI4xIIII8xI" , "4xI8xQQQI12xQ" ) [ is_64bit ] symbol_struct = endian + ( "IIIBBH" , "IBBHQQ" ) [ is_64bit ] header_size = struct . calcsize ( header_struct ) type , machine , version , entry , phoff , shoff , flags , ehsize , phentsize , phnum , shentsize , shnum , shstrndx = struct . unpack_from ( header_struct , elf_data , 16 ) section_offsets = [ ] symbol_tables = [ ] string_tables = { } # Seek to the section header table and find the symbol tables. ptr = shoff for i in range ( shnum ) : type , addr , offset , size , link , entsize = struct . unpack_from ( section_struct , elf_data [ ptr : ptr + shentsize ] ) ptr += shentsize section_offsets . append ( offset - addr ) if type == 0x0B and link != 0 : # SHT_DYNSYM, links to string table symbol_tables . append ( ( offset , size , link , entsize ) ) string_tables [ link ] = None # Read the relevant string tables. for idx in list ( string_tables . keys ( ) ) : ptr = shoff + idx * shentsize type , addr , offset , size , link , entsize = struct . unpack_from ( section_struct , elf_data [ ptr : ptr + shentsize ] ) if type == 3 : string_tables [ idx ] = elf_data [ offset : offset + size ] # Loop through to find the offset of the "blobinfo" symbol. for offset , size , link , entsize in symbol_tables : entries = size // entsize for i in range ( entries ) : ptr = offset + i * entsize fields = struct . unpack_from ( symbol_struct , elf_data [ ptr : ptr + entsize ] ) if is_64bit : name , info , other , shndx , value , size = fields else : name , value , size , info , other , shndx = fields if not name : continue name = string_tables [ link ] [ name : string_tables [ link ] . find ( b'\0' , name ) ] if name == symbol_name : if shndx == 0 : # SHN_UNDEF continue elif shndx >= 0xff00 and shndx <= 0xffff : assert False else : # Got it.  Make the replacement. off = section_offsets [ shndx ] + value elf_data [ off : off + len ( replacement ) ] = replacement replaced = True return replaced