void GarbageCollection::stripSections()
{
  // Traverse all the input Regular and BSS sections, if a section is not found
  // in the ReferencedSections, then it should be garbage collected
  Module::obj_iterator obj, objEnd = m_Module.obj_end();
  for (obj = m_Module.obj_begin(); obj != objEnd; ++obj) {
    LDContext::sect_iterator sect, sectEnd = (*obj)->context()->sectEnd();
    for (sect = (*obj)->context()->sectBegin(); sect != sectEnd; ++sect) {
      LDSection* section = *sect;
      if (LDFileFormat::Regular != section->kind() &&
          LDFileFormat::BSS != section->kind())
        continue;

      if (m_ReferencedSections.find(section) == m_ReferencedSections.end())
        section->setKind(LDFileFormat::Ignore);
    }
  }

  // Traverse all the relocation sections, if its target section is set to
  // Ignore, then set the relocation section to Ignore as well
  Module::obj_iterator input, inEnd = m_Module.obj_end();
  for (input = m_Module.obj_begin(); input != inEnd; ++input) {
    LDContext::sect_iterator rs, rsEnd = (*input)->context()->relocSectEnd();
    for (rs = (*input)->context()->relocSectBegin(); rs != rsEnd; ++rs) {
      LDSection* reloc_sect = *rs;
      if (LDFileFormat::Ignore == reloc_sect->getLink()->kind())
        reloc_sect->setKind(LDFileFormat::Ignore);
    }
  }
}
void GarbageCollection::getEntrySections(SectionVecTy& pEntry)
{
  // all the KEEP sections defined in ldscript are entries, traverse all the
  // input sections and check the SectionMap to find the KEEP sections
  Module::obj_iterator obj, objEnd = m_Module.obj_end();
  SectionMap& sect_map = m_Module.getScript().sectionMap();
  for (obj = m_Module.obj_begin(); obj != objEnd; ++obj) {
    const std::string input_name = (*obj)->name();
    LDContext::sect_iterator sect, sectEnd = (*obj)->context()->sectEnd();
    for (sect = (*obj)->context()->sectBegin(); sect != sectEnd; ++sect) {
      LDSection* section = *sect;
      if (LDFileFormat::Regular != section->kind() &&
          LDFileFormat::BSS != section->kind())
        continue;

      SectionMap::Input* sm_input =
                              sect_map.find(input_name, section->name()).second;
      if ((sm_input != NULL) && (InputSectDesc::Keep == sm_input->policy()))
        pEntry.push_back(section);
    }
  }

  // get the sections those the entry symbols defined in
  Module::SymbolTable& sym_tab = m_Module.getSymbolTable();
  if (LinkerConfig::Exec == m_Config.codeGenType()) {
    assert(NULL != m_pEntry);
    pEntry.push_back(&m_pEntry->fragRef()->frag()->getParent()->getSection());
  }
  else {
    // when building shared objects, the global define symbols are entries
    SymbolCategory::iterator it, end = sym_tab.regularEnd();
    for (it = sym_tab.dynamicBegin(); it != end; ++it) {
      LDSymbol* sym = *it;
      if (!sym->resolveInfo()->isDefine() || !sym->hasFragRef())
        continue;

      // only the target symbols defined in the concerned sections can make
      // the reference
      const LDSection* sect =
                             &sym->fragRef()->frag()->getParent()->getSection();
      if (sect->kind() != LDFileFormat::Regular &&
          sect->kind() != LDFileFormat::BSS)
        continue;

      pEntry.push_back(sect);
    }
  }
}
bool SectionSymbolSet::finalize(LDSection& pOutSect,
                                SymbolTable& pSymTab, bool relocatable)
{
  if (!relocatable && pOutSect.size() == 0)
      return true;

  LDSymbol* sym = get(pOutSect);
  assert(NULL != sym);
  SectionData* data = NULL;
  switch (pOutSect.kind()) {
    case LDFileFormat::Relocation:
      // Relocation section should not have section symbol.
      return true;

    case LDFileFormat::EhFrame:
      if (EhFrame *ehframe = pOutSect.getEhFrame())
          data = ehframe->getSectionData();
      break;

    default:
      data = pOutSect.getSectionData();
      break;
  }
  FragmentRef* frag_ref;
  if (data && !data->empty())
    frag_ref = FragmentRef::Create(data->front(), 0x0);
  else
    frag_ref = FragmentRef::Null();
  sym->setFragmentRef(frag_ref);
  // push symbol into output symbol table
  pSymTab.add(*sym);

  return true;
}
Ejemplo n.º 4
0
void IdenticalCodeFolding::findCandidates(FoldingCandidates& pCandidateList) {
  Module::obj_iterator obj, objEnd = m_Module.obj_end();
  for (obj = m_Module.obj_begin(); obj != objEnd; ++obj) {
    std::set<const LDSection*> funcptr_access_set;
    typedef std::map<LDSection*, LDSection*> CandidateMap;
    CandidateMap candidate_map;
    LDContext::sect_iterator sect, sectEnd = (*obj)->context()->sectEnd();
    for (sect = (*obj)->context()->sectBegin(); sect != sectEnd; ++sect) {
      switch ((*sect)->kind()) {
        case LDFileFormat::TEXT: {
          candidate_map.insert(
              std::make_pair(*sect, reinterpret_cast<LDSection*>(NULL)));
          break;
        }
        case LDFileFormat::Relocation: {
          LDSection* target = (*sect)->getLink();
          if (target->kind() == LDFileFormat::TEXT) {
            candidate_map[target] = *sect;
          }

          // Safe icf
          if (m_Config.options().getICFMode() == GeneralOptions::ICF::Safe) {
            RelocData::iterator rel, relEnd = (*sect)->getRelocData()->end();
            for (rel = (*sect)->getRelocData()->begin(); rel != relEnd; ++rel) {
              LDSymbol* sym = rel->symInfo()->outSymbol();
              if (sym->hasFragRef() && (sym->type() == ResolveInfo::Function)) {
                const LDSection* def =
                    &sym->fragRef()->frag()->getParent()->getSection();
                if (!isSymCtorOrDtor(*rel->symInfo()) &&
                    m_Backend.mayHaveUnsafeFunctionPointerAccess(*target) &&
                    m_Backend.getRelocator()
                        ->mayHaveFunctionPointerAccess(*rel)) {
                  funcptr_access_set.insert(def);
                }
              }
            }  // for each reloc
          }

          break;
        }
        default: {
          // skip
          break;
        }
      }  // end of switch
    }    // for each section

    CandidateMap::iterator candidate, candidateEnd = candidate_map.end();
    for (candidate = candidate_map.begin(); candidate != candidateEnd;
         ++candidate) {
      if ((m_Config.options().getICFMode() == GeneralOptions::ICF::All) ||
          (funcptr_access_set.count(candidate->first) == 0)) {
        size_t index = m_KeptSections.size();
        m_KeptSections[candidate->first] = ObjectAndId(*obj, index);
        pCandidateList.push_back(
            FoldingCandidate(candidate->first, candidate->second, *obj));
      }
    }  // for each possible candidate
  }  // for each obj
}
bool ARMGNULDBackend::readSection(Input& pInput,
                                  MCLinker& pLinker,
                                  LDSection& pInputSectHdr)
{
  LDSection& out_sect = pLinker.getOrCreateOutputSectHdr(pInputSectHdr.name(),
                                                         pInputSectHdr.kind(),
                                                         pInputSectHdr.type(),
                                                         pInputSectHdr.flag());
  // FIXME: (Luba)
  // Handle ARM attributes in the right way.
  // In current milestone, MCLinker goes through the shortcut.
  // It reads input's ARM attributes and copies the first ARM attributes
  // into the output file. The correct way is merge these sections, not
  // just copy.
  if ((0 == out_sect.name().compare(".ARM.attributes")) &&
      (0 != out_sect.size()))
    return true;

  MemoryRegion* region = pInput.memArea()->request(pInputSectHdr.offset(),
                                                   pInputSectHdr.size());

  llvm::MCSectionData& sect_data = pLinker.getOrCreateSectData(pInputSectHdr);

  new MCRegionFragment(*region, &sect_data);

  out_sect.setSize(out_sect.size() + pInputSectHdr.size());
  return true;
}
Ejemplo n.º 6
0
/// merge Input Sections
bool HexagonLDBackend::mergeSection(Module& pModule,
                                    const Input& pInputFile,
                                    LDSection& pInputSection) {
  if ((pInputSection.flag() & llvm::ELF::SHF_HEX_GPREL) ||
      (pInputSection.kind() == LDFileFormat::LinkOnce) ||
      (pInputSection.kind() == LDFileFormat::Target)) {
    SectionData* sd = NULL;
    if (!m_psdata->hasSectionData()) {
      sd = IRBuilder::CreateSectionData(*m_psdata);
      m_psdata->setSectionData(sd);
    }
    sd = m_psdata->getSectionData();
    MoveSectionDataAndSort(*pInputSection.getSectionData(), *sd);
  } else {
    ObjectBuilder builder(pModule);
    builder.MergeSection(pInputFile, pInputSection);
  }
  return true;
}
Ejemplo n.º 7
0
/// emitSectionData
void
ELFObjectWriter::emitSectionData(const LDSection& pSection,
                                 MemoryRegion& pRegion) const
{
  const SectionData* sd = NULL;
  switch (pSection.kind()) {
    case LDFileFormat::Relocation:
      assert(pSection.hasRelocData());
      return;
    case LDFileFormat::EhFrame:
      assert(pSection.hasEhFrame());
      sd = pSection.getEhFrame()->getSectionData();
      break;
    default:
      assert(pSection.hasSectionData());
      sd = pSection.getSectionData();
      break;
  }
  emitSectionData(*sd, pRegion);
}
Ejemplo n.º 8
0
void IdenticalCodeFolding::foldIdenticalCode() {
  // 1. Find folding candidates.
  FoldingCandidates candidate_list;
  findCandidates(candidate_list);

  // 2. Initialize constant section content
  for (size_t i = 0; i < candidate_list.size(); ++i) {
    candidate_list[i].initConstantContent(m_Backend, m_KeptSections);
  }

  // 3. Find identical code until convergence
  bool converged = false;
  size_t iterations = 0;
  while (!converged && (iterations < m_Config.options().getICFIterations())) {
    converged = matchCandidates(candidate_list);
    ++iterations;
  }
  if (m_Config.options().printICFSections()) {
    debug(diag::debug_icf_iterations) << iterations;
  }

  // 4. Fold the identical code
  typedef std::set<Input*> FoldedObjects;
  FoldedObjects folded_objs;
  KeptSections::iterator kept, keptEnd = m_KeptSections.end();
  size_t index = 0;
  for (kept = m_KeptSections.begin(); kept != keptEnd; ++kept, ++index) {
    LDSection* sect = (*kept).first;
    Input* obj = (*kept).second.first;
    size_t kept_index = (*kept).second.second;
    if (index != kept_index) {
      sect->setKind(LDFileFormat::Folded);
      folded_objs.insert(obj);

      if (m_Config.options().printICFSections()) {
        KeptSections::iterator it = m_KeptSections.begin() + kept_index;
        LDSection* kept_sect = (*it).first;
        Input* kept_obj = (*it).second.first;
        debug(diag::debug_icf_folded_section) << sect->name() << obj->name()
                                              << kept_sect->name()
                                              << kept_obj->name();
      }
    }
  }

  // Adjust the fragment reference of the folded symbols.
  FoldedObjects::iterator fobj, fobjEnd = folded_objs.end();
  for (fobj = folded_objs.begin(); fobj != fobjEnd; ++fobj) {
    LDContext::sym_iterator sym, symEnd = (*fobj)->context()->symTabEnd();
    for (sym = (*fobj)->context()->symTabBegin(); sym != symEnd; ++sym) {
      if ((*sym)->hasFragRef() && ((*sym)->type() == ResolveInfo::Function)) {
        LDSymbol* out_sym = (*sym)->resolveInfo()->outSymbol();
        FragmentRef* frag_ref = out_sym->fragRef();
        LDSection* sect = &(frag_ref->frag()->getParent()->getSection());
        if (sect->kind() == LDFileFormat::Folded) {
          size_t kept_index = m_KeptSections[sect].second;
          LDSection* kept_sect = (*(m_KeptSections.begin() + kept_index)).first;
          frag_ref->assign(kept_sect->getSectionData()->front(),
                           frag_ref->offset());
        }
      }
    }  // for each symbol
  }    // for each folded object
}
Ejemplo n.º 9
0
bool MipsGNULDBackend::doRelax(Module& pModule,
                               IRBuilder& pBuilder,
                               bool& pFinished) {
  assert(getStubFactory() != NULL && getBRIslandFactory() != NULL);

  bool isRelaxed = false;

  for (Module::obj_iterator input = pModule.obj_begin();
       input != pModule.obj_end();
       ++input) {
    LDContext* context = (*input)->context();

    for (LDContext::sect_iterator rs = context->relocSectBegin();
         rs != context->relocSectEnd();
         ++rs) {
      LDSection* sec = *rs;

      if (LDFileFormat::Ignore == sec->kind() || !sec->hasRelocData())
        continue;

      for (RelocData::iterator reloc = sec->getRelocData()->begin();
           reloc != sec->getRelocData()->end();
           ++reloc) {
        if (llvm::ELF::R_MIPS_26 != reloc->type())
          continue;

        if (relaxRelocation(pBuilder, *llvm::cast<Relocation>(reloc)))
          isRelaxed = true;
      }
    }
  }

  SectionData* textData = getOutputFormat()->getText().getSectionData();

  // find the first fragment w/ invalid offset due to stub insertion
  Fragment* invalid = NULL;
  pFinished = true;
  for (BranchIslandFactory::iterator ii = getBRIslandFactory()->begin(),
                                     ie = getBRIslandFactory()->end();
       ii != ie;
       ++ii) {
    BranchIsland& island = *ii;
    if (island.end() == textData->end())
      break;

    Fragment* exit = island.end();
    if ((island.offset() + island.size()) > exit->getOffset()) {
      invalid = exit;
      pFinished = false;
      break;
    }
  }

  // reset the offset of invalid fragments
  while (invalid != NULL) {
    invalid->setOffset(invalid->getPrevNode()->getOffset() +
                       invalid->getPrevNode()->size());
    invalid = invalid->getNextNode();
  }

  // reset the size of .text
  if (isRelaxed)
    getOutputFormat()->getText().setSize(textData->back().getOffset() +
                                         textData->back().size());

  return isRelaxed;
}