def linker_script_segments(script): file_ = tempfile.TemporaryFile() file_.write(script) file_.seek(0) return extract_segment_names(file_)
def get_segment_names(options, elf, scripts, linker_name, orphans = []): """ Return a list of names that correspond to the segments in the ELF file. The names can come from the command line, a file, the program headers listed in a linker script or generated from the flag values of the segments themselves. """ seglist = [] if len(scripts) != 0: scripts = scripts[0] # segments specifed in a file if options.file_segment_list: segfile = open(options.file_segment_list, 'r') seglist = pad_non_loaded(elf, segfile.readlines()) # segments specified on command line elif options.cmd_segment_list: seglist = pad_non_loaded(elf, options.cmd_segment_list.split(r',')) # get segments from linker script elif len(scripts) != 0 and linker_name == "rvct": segments_to_sections_dict = rvct_parser.extract_segment_names(scripts) # sections may appear in more than one segment # the correct segment in the linker script seglist = seg_list_rvct (elf, segments_to_sections_dict) # get segments from linker script elif len(scripts) != 0 and linker_name == "gcc": seglist = gnu_parser.extract_segment_names(scripts) # If no segment names are found, then generate default names. # Loadable segments are named according to their ELF flags. Other # segments are unnamed because they are unused by elfweaver. if len(seglist) == 0: for seg in elf.segments: seg_name = "" if seg.has_sections(): for sect in seg.get_sections(): if sect.name in orphans: seg_name = sect.name if seg_name == "": if seg.flags & PF_R: seg_name += 'r' if seg.flags & PF_W: seg_name += 'w' if seg.flags & PF_X: seg_name += 'x' if seg_name == "": seg_name = "??" seglist.append(seg_name) return seglist
def get_segment_names(elf, seglist=None, scripts=None, linker_name=None, orphans=None): """ Return a list of names that correspond to the segments in the ELF file. The names can come from the command line, a file, the program headers listed in a linker script or generated from the flag values of the segments themselves. """ if scripts is not None and len(scripts) != 0: scripts = scripts[0] if orphans is None: orphans = [] # segments specifed in a file if seglist is not None: seglist = _pad_non_loaded(elf, seglist) # get segments from linker script elif scripts is not None and len(scripts) != 0 and linker_name == "rvct": segments_to_sections_dict = rvct_parser.extract_segment_names(scripts) # sections may appear in more than one segment # the correct segment in the linker script seglist = _seg_list_rvct(elf, segments_to_sections_dict) # get segments from linker script elif scripts is not None and len(scripts) != 0 and linker_name == "gcc": seglist = gnu_parser.extract_segment_names(scripts) # otherwise we use the defaults else: seglist = [] # If no segment names are found, then generate default names. # Loadable segments are named according to their ELF flags. Other # segments are unnamed because they are unused by elfweaver. if len(seglist) < len(elf.segments): seglist_enum = [] segcount = 0 for seg in elf.segments[len(seglist) :]: seg_name = "" if seg.has_sections(): for sect in seg.get_sections(): if sect.name in orphans: seg_name = sect.name if seg_name == "": if seg.flags & PF_R: seg_name += "r" if seg.flags & PF_W: seg_name += "w" if seg.flags & PF_X: seg_name += "x" if seg_name == "": seg_name = "??" seglist_enum.append((segcount, seg_name)) segcount += 1 # Find and flag all segments which have non-unique flags counts = {} for seg in elf.segments: counts[seg.flags] = counts.get(seg.flags, 0) + 1 duplicates = [counts[seg.flags] > 1 for seg in elf.segments] # For all segments which have duplicate names: # First try to append the name of their first section # Failing that, append their segment number # (which is actually its position in the segment lsit) # Finally, construct the seglist that will be returned for i, seg_name in seglist_enum: is_dup = duplicates[i] if is_dup == True and seg_name not in orphans: seg_i = elf.segments[i] if seg_i.has_sections(): first_sect_name = seg_i.get_sections()[0].name if first_sect_name.startswith("."): seg_name += "-" + first_sect_name.split(".")[1] else: seg_name += "-" + first_sect_name else: seg_name += "-%02d" % i seglist.append(seg_name) return seglist
def get_segment_names(options, elf, scripts, linker_name, orphans=[]): """ Return a list of names that correspond to the segments in the ELF file. The names can come from the command line, a file, the program headers listed in a linker script or generated from the flag values of the segments themselves. """ seglist = [] if len(scripts) != 0: scripts = scripts[0] # segments specifed in a file if options.file_segment_list: segfile = open(options.file_segment_list, 'r') seglist = pad_non_loaded(elf, segfile.readlines()) # segments specified on command line elif options.cmd_segment_list: seglist = pad_non_loaded(elf, options.cmd_segment_list.split(r',')) # get segments from linker script elif len(scripts) != 0 and linker_name == "rvct": segments_to_sections_dict = rvct_parser.extract_segment_names(scripts) # sections may appear in more than one segment # the correct segment in the linker script seglist = seg_list_rvct(elf, segments_to_sections_dict) # get segments from linker script elif len(scripts) != 0 and linker_name == "gcc": seglist = gnu_parser.extract_segment_names(scripts) # If no segment names are found, then generate default names. # Loadable segments are named according to their ELF flags. Other # segments are unnamed because they are unused by elfweaver. if len(seglist) == 0: for seg in elf.segments: seg_name = "" if seg.has_sections(): for sect in seg.get_sections(): if sect.name in orphans: seg_name = sect.name if seg_name == "": if seg.flags & PF_R: seg_name += 'r' if seg.flags & PF_W: seg_name += 'w' if seg.flags & PF_X: seg_name += 'x' if seg_name == "": seg_name = "??" seglist.append(seg_name) return seglist
def get_segment_names(elf, seglist=None, scripts=None, linker_name=None, orphans=None): """ Return a list of names that correspond to the segments in the ELF file. The names can come from the command line, a file, the program headers listed in a linker script or generated from the flag values of the segments themselves. """ if scripts is not None and len(scripts) != 0: scripts = scripts[0] if orphans is None: orphans = [] # segments specifed in a file if seglist is not None: seglist = _pad_non_loaded(elf, seglist) # get segments from linker script elif scripts is not None and len(scripts) != 0 and linker_name == "rvct": segments_to_sections_dict = rvct_parser.extract_segment_names(scripts) # sections may appear in more than one segment # the correct segment in the linker script seglist = _seg_list_rvct(elf, segments_to_sections_dict) # get segments from linker script elif scripts is not None and len(scripts) != 0 and linker_name == "gcc": seglist = gnu_parser.extract_segment_names(scripts) # otherwise we use the defaults else: seglist = [] # If no segment names are found, then generate default names. # Loadable segments are named according to their ELF flags. Other # segments are unnamed because they are unused by elfweaver. if len(seglist) < len(elf.segments): seglist_enum = [] segcount = 0 for seg in elf.segments[len(seglist):]: seg_name = "" if seg.has_sections(): for sect in seg.get_sections(): if sect.name in orphans: seg_name = sect.name if seg_name == "": if seg.flags & PF_R: seg_name += 'r' if seg.flags & PF_W: seg_name += 'w' if seg.flags & PF_X: seg_name += 'x' if seg_name == "": seg_name = "??" seglist_enum.append((segcount, seg_name)) segcount += 1 # Find and flag all segments which have non-unique flags counts = {} for seg in elf.segments: counts[seg.flags] = counts.get(seg.flags, 0) + 1 duplicates = [counts[seg.flags] > 1 for seg in elf.segments] # For all segments which have duplicate names: # First try to append the name of their first section # Failing that, append their segment number # (which is actually its position in the segment lsit) # Finally, construct the seglist that will be returned for i, seg_name in seglist_enum: is_dup = duplicates[i] if is_dup == True and seg_name not in orphans: seg_i = elf.segments[i] if seg_i.has_sections(): first_sect_name = seg_i.get_sections()[0].name if first_sect_name.startswith("."): seg_name += "-" + first_sect_name.split(".")[1] else: seg_name += "-" + first_sect_name else: seg_name += "-%02d" % i seglist.append(seg_name) return seglist