#include <stdint.h>
#include <math.h>
#include <unistd.h>
#include <dlfcn.h>
#include <vector>
#include <map>
#include "ld.hpp"
#include "got.h"
namespace ld {
namespace passes {
namespace got {
class File;
class GOTEntryAtom : public ld::Atom {
public:
GOTEntryAtom(ld::Internal& internal, const ld::Atom* target, bool weakImport)
: ld::Atom(_s_section, ld::Atom::definitionRegular, ld::Atom::combineNever,
ld::Atom::scopeLinkageUnit, ld::Atom::typeNonLazyPointer,
symbolTableNotIn, false, false, false, ld::Atom::Alignment(3)),
_fixup(0, ld::Fixup::k1of1, ld::Fixup::kindStoreTargetAddressLittleEndian64, target),
_target(target)
{ _fixup.weakImport = weakImport; internal.addAtom(*this); }
virtual const ld::File* file() const { return NULL; }
virtual const char* name() const { return _target->name(); }
virtual uint64_t size() const { return 8; }
virtual uint64_t objectAddress() const { return 0; }
virtual void copyRawContent(uint8_t buffer[]) const { }
virtual void setScope(Scope) { }
virtual ld::Fixup::iterator fixupsBegin() const { return &_fixup; }
virtual ld::Fixup::iterator fixupsEnd() const { return &((ld::Fixup*)&_fixup)[1]; }
private:
mutable ld::Fixup _fixup;
const ld::Atom* _target;
static ld::Section _s_section;
};
ld::Section GOTEntryAtom::_s_section("__DATA", "__got", ld::Section::typeNonLazyPointer);
static bool gotFixup(const Options& opts, ld::Internal& internal, const ld::Atom* targetOfGOT, const ld::Fixup* fixup, bool* optimizable)
{
switch (fixup->kind) {
case ld::Fixup::kindStoreTargetAddressX86PCRel32GOTLoad:
*optimizable = true;
if ( targetOfGOT->definition() == ld::Atom::definitionProxy )
*optimizable = false;
if ( internal.usingHugeSections && (targetOfGOT->size() > 1024*1024)
&& ( (targetOfGOT->section().type() == ld::Section::typeZeroFill)
|| (targetOfGOT->section().type() == ld::Section::typeTentativeDefs)) ) {
*optimizable = false;
}
if ( targetOfGOT->scope() == ld::Atom::scopeGlobal ) {
if ( (targetOfGOT->definition() == ld::Atom::definitionRegular) && (targetOfGOT->combine() == ld::Atom::combineByName) ) {
switch ( opts.outputKind() ) {
case Options::kDynamicExecutable:
case Options::kDynamicLibrary:
case Options::kDynamicBundle:
case Options::kKextBundle:
*optimizable = false;
break;
case Options::kStaticExecutable:
case Options::kDyld:
case Options::kPreload:
case Options::kObjectFile:
break;
}
}
if ( opts.interposable(targetOfGOT->name()) )
*optimizable = false;
if ( targetOfGOT->contentType() == ld::Atom::typeResolver )
*optimizable = false;
if ( opts.nameSpace() != Options::kTwoLevelNameSpace )
*optimizable = false;
}
else if ( targetOfGOT->scope() == ld::Atom::scopeLinkageUnit) {
if ( opts.sharedRegionEligible() ) {
const char* segName = targetOfGOT->section().segmentName();
if ( (strcmp(segName, "__TEXT") != 0) && (strcmp(segName, "__DATA") != 0) ) {
*optimizable = false;
}
}
}
return true;
case ld::Fixup::kindStoreX86PCRel32GOT:
*optimizable = false;
return true;
case ld::Fixup::kindNoneGroupSubordinatePersonality:
*optimizable = false;
return true;
default:
break;
}
return false;
}
struct AtomByNameSorter
{
bool operator()(const ld::Atom* left, const ld::Atom* right)
{
return (strcmp(left->name(), right->name()) < 0);
}
};
void doPass(const Options& opts, ld::Internal& internal)
{
const bool log = false;
if ( opts.outputKind() == Options::kObjectFile )
return;
std::vector<const ld::Atom*> atomsReferencingGOT;
std::map<const ld::Atom*,ld::Atom*> gotMap;
std::map<const ld::Atom*,bool> weakImportMap;
atomsReferencingGOT.reserve(128);
for (std::vector<ld::Internal::FinalSection*>::iterator sit=internal.sections.begin(); sit != internal.sections.end(); ++sit) {
ld::Internal::FinalSection* sect = *sit;
for (std::vector<const ld::Atom*>::iterator ait=sect->atoms.begin(); ait != sect->atoms.end(); ++ait) {
const ld::Atom* atom = *ait;
bool atomUsesGOT = false;
const ld::Atom* targetOfGOT = NULL;
bool targetIsWeakImport = false;
for (ld::Fixup::iterator fit = atom->fixupsBegin(), end=atom->fixupsEnd(); fit != end; ++fit) {
if ( fit->firstInCluster() )
targetOfGOT = NULL;
switch ( fit->binding ) {
case ld::Fixup::bindingsIndirectlyBound:
targetOfGOT = internal.indirectBindingTable[fit->u.bindingIndex];
targetIsWeakImport = fit->weakImport;
break;
case ld::Fixup::bindingDirectlyBound:
targetOfGOT = fit->u.target;
targetIsWeakImport = fit->weakImport;
break;
default:
break;
}
bool optimizable;
if ( !gotFixup(opts, internal, targetOfGOT, fit, &optimizable) )
continue;
if ( optimizable ) {
if ( log ) fprintf(stderr, "optimized GOT usage in %s to %s\n", atom->name(), targetOfGOT->name());
switch ( fit->binding ) {
case ld::Fixup::bindingsIndirectlyBound:
case ld::Fixup::bindingDirectlyBound:
fit->binding = ld::Fixup::bindingDirectlyBound;
fit->u.target = targetOfGOT;
fit->kind = ld::Fixup::kindStoreTargetAddressX86PCRel32GOTLoadNowLEA;
break;
default:
assert(0 && "unsupported GOT reference");
break;
}
}
else {
if ( log ) fprintf(stderr, "found GOT use in %s to %s\n", atom->name(), targetOfGOT->name());
if ( !atomUsesGOT ) {
atomsReferencingGOT.push_back(atom);
atomUsesGOT = true;
}
gotMap[targetOfGOT] = NULL;
std::map<const ld::Atom*,bool>::iterator pos = weakImportMap.find(targetOfGOT);
if ( pos == weakImportMap.end() ) {
if ( log ) fprintf(stderr, "weakImportMap[%s] = %d\n", targetOfGOT->name(), targetIsWeakImport);
weakImportMap[targetOfGOT] = targetIsWeakImport;
}
else {
if ( pos->second != targetIsWeakImport ) {
switch ( opts.weakReferenceMismatchTreatment() ) {
case Options::kWeakReferenceMismatchError:
throwf("mismatching weak references for symbol: %s", targetOfGOT->name());
case Options::kWeakReferenceMismatchWeak:
pos->second = true;
break;
case Options::kWeakReferenceMismatchNonWeak:
pos->second = false;
break;
}
}
}
}
}
}
}
for (std::map<const ld::Atom*,ld::Atom*>::iterator it = gotMap.begin(); it != gotMap.end(); ++it) {
it->second = new GOTEntryAtom(internal, it->first, weakImportMap[it->first]);
}
for (std::vector<const ld::Atom*>::iterator it=atomsReferencingGOT.begin(); it != atomsReferencingGOT.end(); ++it) {
const ld::Atom* atom = *it;
const ld::Atom* targetOfGOT = NULL;
ld::Fixup::iterator fitThatSetTarget = NULL;
for (ld::Fixup::iterator fit = atom->fixupsBegin(), end=atom->fixupsEnd(); fit != end; ++fit) {
if ( fit->firstInCluster() ) {
targetOfGOT = NULL;
fitThatSetTarget = NULL;
}
switch ( fit->binding ) {
case ld::Fixup::bindingsIndirectlyBound:
targetOfGOT = internal.indirectBindingTable[fit->u.bindingIndex];
fitThatSetTarget = fit;
break;
case ld::Fixup::bindingDirectlyBound:
targetOfGOT = fit->u.target;
fitThatSetTarget = fit;
break;
default:
break;
}
bool optimizable;
if ( (targetOfGOT == NULL) || !gotFixup(opts, internal, targetOfGOT, fit, &optimizable) )
continue;
if ( !optimizable ) {
assert(fitThatSetTarget != NULL);
switch ( fitThatSetTarget->binding ) {
case ld::Fixup::bindingsIndirectlyBound:
case ld::Fixup::bindingDirectlyBound:
fitThatSetTarget->binding = ld::Fixup::bindingDirectlyBound;
fitThatSetTarget->u.target = gotMap[targetOfGOT];
break;
default:
assert(0 && "unsupported GOT reference");
break;
}
}
}
}
for (std::vector<ld::Internal::FinalSection*>::iterator sit=internal.sections.begin(); sit != internal.sections.end(); ++sit) {
ld::Internal::FinalSection* sect = *sit;
if ( sect->type() == ld::Section::typeNonLazyPointer ) {
std::sort(sect->atoms.begin(), sect->atoms.end(), AtomByNameSorter());
}
}
}
} } }