Skip to content

Commit

Permalink
Revert "Revert "Revert "[LLD][ELF] Cortex-M Security Extensions (CMSE…
Browse files Browse the repository at this point in the history
…) Support"""

This reverts commit 9246df7.

Reason: This patch broke the UBSan buildbots. See more information in
the original phabricator review: https://reviews.llvm.org/D139092
  • Loading branch information
hctim committed Jun 22, 2023
1 parent c859231 commit cd116e0
Show file tree
Hide file tree
Showing 22 changed files with 2 additions and 1,215 deletions.
410 changes: 0 additions & 410 deletions lld/ELF/Arch/ARM.cpp

Large diffs are not rendered by default.

5 changes: 0 additions & 5 deletions lld/ELF/Config.h
Expand Up @@ -129,7 +129,6 @@ class LinkerDriver {

std::unique_ptr<BitcodeCompiler> lto;
std::vector<InputFile *> files;
std::optional<InputFile *> armCmseImpLib;

public:
SmallVector<std::pair<StringRef, unsigned>, 0> archiveFiles;
Expand Down Expand Up @@ -174,8 +173,6 @@ struct Config {
llvm::StringRef thinLTOCacheDir;
llvm::StringRef thinLTOIndexOnlyArg;
llvm::StringRef whyExtract;
llvm::StringRef cmseInputLib;
llvm::StringRef cmseOutputLib;
StringRef zBtiReport = "none";
StringRef zCetReport = "none";
llvm::StringRef ltoBasicBlockSections;
Expand All @@ -198,13 +195,11 @@ struct Config {
llvm::MapVector<std::pair<const InputSectionBase *, const InputSectionBase *>,
uint64_t>
callGraphProfile;
bool cmseImplib = false;
bool allowMultipleDefinition;
bool androidPackDynRelocs = false;
bool armHasBlx = false;
bool armHasMovtMovw = false;
bool armJ1J2BranchEncoding = false;
bool armCMSESupport = false;
bool asNeeded = false;
BsymbolicKind bsymbolic = BsymbolicKind::None;
bool callGraphProfileSort;
Expand Down
30 changes: 0 additions & 30 deletions lld/ELF/Driver.cpp
Expand Up @@ -345,22 +345,6 @@ static void checkOptions() {
if (config->emachine == EM_MIPS && config->gnuHash)
error("the .gnu.hash section is not compatible with the MIPS target");

if (config->emachine == EM_ARM) {
if (!config->cmseImplib) {
if (!config->cmseInputLib.empty())
error("--in-implib may not be used without --cmse-implib");
if (!config->cmseOutputLib.empty())
error("--out-implib may not be used without --cmse-implib");
}
} else {
if (config->cmseImplib)
error("--cmse-implib is only supported on ARM targets");
if (!config->cmseInputLib.empty())
error("--in-implib is only supported on ARM targets");
if (!config->cmseOutputLib.empty())
error("--out-implib is only supported on ARM targets");
}

if (config->fixCortexA53Errata843419 && config->emachine != EM_AARCH64)
error("--fix-cortex-a53-843419 is only supported on AArch64 targets");

Expand Down Expand Up @@ -1177,9 +1161,6 @@ static void readConfigs(opt::InputArgList &args) {
config->fini = args.getLastArgValue(OPT_fini, "_fini");
config->fixCortexA53Errata843419 = args.hasArg(OPT_fix_cortex_a53_843419) &&
!args.hasArg(OPT_relocatable);
config->cmseImplib = args.hasArg(OPT_cmse_implib);
config->cmseInputLib = args.getLastArgValue(OPT_in_implib);
config->cmseOutputLib = args.getLastArgValue(OPT_out_implib);
config->fixCortexA8 =
args.hasArg(OPT_fix_cortex_a8) && !args.hasArg(OPT_relocatable);
config->fortranCommon =
Expand Down Expand Up @@ -1757,12 +1738,6 @@ void LinkerDriver::createFiles(opt::InputArgList &args) {
files.back()->justSymbols = true;
}
break;
case OPT_in_implib:
if (armCmseImpLib)
error("multiple CMSE import libraries not supported");
else if (std::optional<MemoryBufferRef> mb = readFile(arg->getValue()))
armCmseImpLib = createObjFile(*mb);
break;
case OPT_start_group:
if (InputFile::isInGroup)
error("nested --start-group");
Expand Down Expand Up @@ -2642,8 +2617,6 @@ void LinkerDriver::link(opt::InputArgList &args) {
llvm::TimeTraceScope timeScope("Parse input files", files[i]->getName());
parseFile(files[i]);
}
if (armCmseImpLib)
parseArmCMSEImportLib(*armCmseImpLib);
}

// Now that we have every file, we can decide if we will need a
Expand Down Expand Up @@ -2808,9 +2781,6 @@ void LinkerDriver::link(opt::InputArgList &args) {
if (args.hasArg(OPT_exclude_libs))
excludeLibs(args);

// Record [__acle_se_<sym>, <sym>] pairs for later processing.
processArmCmseSymbols();

// Apply symbol renames for --wrap and combine foo@v1 and foo@@v1.
redirectSymbols(wrapped);

Expand Down
21 changes: 2 additions & 19 deletions lld/ELF/InputFiles.cpp
Expand Up @@ -177,15 +177,6 @@ static void updateSupportedARMFeatures(const ARMAttributeParser &attributes) {
config->armHasMovtMovw = true;
break;
}

// Only ARMv8-M or later architectures have CMSE support.
std::optional<unsigned> profile =
attributes.getAttributeValue(ARMBuildAttrs::CPU_arch_profile);
if (!profile)
return;
if (arch >= ARMBuildAttrs::CPUArch::v8_M_Base &&
profile == ARMBuildAttrs::MicroControllerProfile)
config->armCMSESupport = true;
}

InputFile::InputFile(Kind k, MemoryBufferRef m)
Expand Down Expand Up @@ -326,14 +317,6 @@ template <class ELFT> static void doParseFile(InputFile *file) {
// Add symbols in File to the symbol table.
void elf::parseFile(InputFile *file) { invokeELFT(doParseFile, file); }

template <class ELFT> static void doParseArmCMSEImportLib(InputFile *file) {
cast<ObjFile<ELFT>>(file)->importCmseSymbols();
}

void elf::parseArmCMSEImportLib(InputFile *file) {
invokeELFT(doParseArmCMSEImportLib, file);
}

// Concatenates arguments to construct a string representing an error location.
static std::string createFileLineMsg(StringRef path, unsigned line) {
std::string filename = std::string(path::filename(path));
Expand Down Expand Up @@ -1048,8 +1031,8 @@ InputSectionBase *ObjFile<ELFT>::createInputSection(uint32_t idx,
return makeThreadLocal<InputSection>(*this, sec, name);
}

// Initialize symbols. symbols is a parallel array to the corresponding ELF
// symbol table.
// Initialize this->Symbols. this->Symbols is a parallel array as
// its corresponding ELF symbol table.
template <class ELFT>
void ObjFile<ELFT>::initializeSymbols(const object::ELFFile<ELFT> &obj) {
ArrayRef<Elf_Sym> eSyms = this->getELFSyms<ELFT>();
Expand Down
10 changes: 0 additions & 10 deletions lld/ELF/InputFiles.h
Expand Up @@ -48,8 +48,6 @@ std::optional<MemoryBufferRef> readFile(StringRef path);
// Add symbols in File to the symbol table.
void parseFile(InputFile *file);

void parseArmCMSEImportLib(InputFile *file);

// The root class of input files.
class InputFile {
protected:
Expand Down Expand Up @@ -90,12 +88,6 @@ class InputFile {
return {symbols.get(), numSymbols};
}

MutableArrayRef<Symbol *> getMutableSymbols() {
assert(fileKind == BinaryKind || fileKind == ObjKind ||
fileKind == BitcodeKind);
return {symbols.get(), numSymbols};
}

// Get filename to use for linker script processing.
StringRef getNameForScript() const;

Expand Down Expand Up @@ -288,8 +280,6 @@ template <class ELFT> class ObjFile : public ELFFileBase {

void initSectionsAndLocalSyms(bool ignoreComdats);
void postParse();
void importCmseSymbols();
void redirectCmseSymbols();

private:
void initializeSections(bool ignoreComdats,
Expand Down
9 changes: 0 additions & 9 deletions lld/ELF/LinkerScript.cpp
Expand Up @@ -896,15 +896,6 @@ void LinkerScript::diagnoseOrphanHandling() const {
}
}

void LinkerScript::diagnoseMissingSGSectionAddress() const {
if (!config->cmseImplib || !in.armCmseSGSection->isNeeded())
return;

OutputSection *sec = findByName(sectionCommands, ".gnu.sgstubs");
if (sec && !sec->addrExpr && !config->sectionStartMap.count(".gnu.sgstubs"))
error("no address assigned to the veneers output section " + sec->name);
}

// This function searches for a memory region to place the given output
// section in. If found, a pointer to the appropriate memory region is
// returned in the first member of the pair. Otherwise, a nullptr is returned.
Expand Down
1 change: 0 additions & 1 deletion lld/ELF/LinkerScript.h
Expand Up @@ -318,7 +318,6 @@ class LinkerScript final {

void addOrphanSections();
void diagnoseOrphanHandling() const;
void diagnoseMissingSGSectionAddress() const;
void adjustOutputSections();
void adjustSectionsAfterSorting();

Expand Down
4 changes: 0 additions & 4 deletions lld/ELF/MarkLive.cpp
Expand Up @@ -230,10 +230,6 @@ template <class ELFT> void MarkLive<ELFT>::run() {
markSymbol(symtab.find(s));
for (StringRef s : script->referencedSymbols)
markSymbol(symtab.find(s));
for (auto [symName, _] : symtab.cmseSymMap) {
markSymbol(symtab.cmseSymMap[symName].sym);
markSymbol(symtab.cmseSymMap[symName].acleSeSym);
}

// Mark .eh_frame sections as live because there are usually no relocations
// that point to .eh_frames. Otherwise, the garbage collector would drop
Expand Down
12 changes: 0 additions & 12 deletions lld/ELF/Options.td
Expand Up @@ -80,18 +80,6 @@ defm split_stack_adjust_size

def O: JoinedOrSeparate<["-"], "O">, HelpText<"Optimize output file size">;

def cmse_implib: FF<"cmse-implib">,
HelpText<"Make the output library to be a CMSE secure code import library">;

defm in_implib: EEq<"in-implib",
"Read an existing CMSE secure code import library and preserve entry function addresses in the "
"resulting new CMSE secure code import library (optional when creating a CMSE secure image)">,
MetaVarName<"<file>">;

defm out_implib: EEq<"out-implib",
"Output the CMSE secure code import library to <file> (required when creating a CMSE secure image)">,
MetaVarName<"<file>">;

defm Tbss: Eq<"Tbss", "Same as --section-start with .bss as the sectionname">;

defm Tdata: Eq<"Tdata", "Same as --section-start with .data as the sectionname">;
Expand Down
17 changes: 0 additions & 17 deletions lld/ELF/SymbolTable.h
Expand Up @@ -19,11 +19,6 @@ namespace lld::elf {
class InputFile;
class SharedFile;

struct ArmCmseEntryFunction {
Symbol *acleSeSym;
Symbol *sym;
};

// SymbolTable is a bucket of all known symbols, including defined,
// undefined, or lazy symbols (the last one is symbols in archive
// files whose archive members are not yet loaded).
Expand Down Expand Up @@ -65,18 +60,6 @@ class SymbolTable {
// is used to uniquify them.
llvm::DenseMap<llvm::CachedHashStringRef, const InputFile *> comdatGroups;

// The Map of __acle_se_<sym>, <sym> pairs found in the input objects.
// Key is the <sym> name.
llvm::SmallMapVector<StringRef, ArmCmseEntryFunction, 1> cmseSymMap;

// Map of symbols defined in the Arm CMSE import library. The linker must
// preserve the addresses in the output objects.
llvm::StringMap<Defined *> cmseImportLib;

// True if <sym> from the input Arm CMSE import library is written to the
// output Arm CMSE import library.
llvm::StringMap<bool> inCMSEOutImpLib;

private:
SmallVector<Symbol *, 0> findByVersion(SymbolVersion ver);
SmallVector<Symbol *, 0> findAllByVersion(SymbolVersion ver,
Expand Down
1 change: 0 additions & 1 deletion lld/ELF/SyntheticSections.cpp
Expand Up @@ -3828,7 +3828,6 @@ void InStruct::reset() {
got.reset();
gotPlt.reset();
igotPlt.reset();
armCmseSGSection.reset();
ppc64LongBranchTarget.reset();
mipsAbiFlags.reset();
mipsGot.reset();
Expand Down
46 changes: 0 additions & 46 deletions lld/ELF/SyntheticSections.h
Expand Up @@ -1143,51 +1143,6 @@ class ThunkSection final : public SyntheticSection {
size_t size = 0;
};

// Cortex-M Security Extensions. Prefix for functions that should be exported
// for the non-secure world.
const char ACLESESYM_PREFIX[] = "__acle_se_";
const int ACLESESYM_SIZE = 8;

class ArmCmseSGVeneer : public SyntheticSection {
public:
ArmCmseSGVeneer(Symbol *sym, Symbol *acleSeSym,
std::optional<uint64_t> addr = std::nullopt)
: SyntheticSection(llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_EXECINSTR,
llvm::ELF::SHT_PROGBITS,
/*alignment=*/32, ".gnu.sgstubs"),
sym(sym), acleSeSym(acleSeSym), entAddr{addr} {
entsize = ACLESESYM_SIZE;
}

void writeTo(uint8_t *buf) override;
size_t getSize() const override { return entsize; };
const std::optional<uint64_t> getAddr() const { return entAddr; };

Symbol *sym;
Symbol *acleSeSym;

private:
const std::optional<uint64_t> entAddr;
};

class ArmCmseSGSection : public SyntheticSection {
public:
ArmCmseSGSection();
bool isNeeded() const override { return !entries.empty(); }
size_t getSize() const override;
void writeTo(uint8_t *buf) override;
void addSGVeneer(Symbol *sym, Symbol *ext_sym);
void addMappingSymbol();
void finalizeContents() override;
void exportEntries(SymbolTableBaseSection *symTab);
uint64_t impLibMaxAddr = 0;

private:
SmallVector<std::pair<Symbol *, Symbol *>, 0> entries;
SmallVector<ArmCmseSGVeneer *, 0> sgSections;
uint64_t newEntries = 0;
};

// Used to compute outSecOff of .got2 in each object file. This is needed to
// synthesize PLT entries for PPC32 Secure PLT ABI.
class PPC32Got2Section final : public SyntheticSection {
Expand Down Expand Up @@ -1324,7 +1279,6 @@ struct InStruct {
std::unique_ptr<GotSection> got;
std::unique_ptr<GotPltSection> gotPlt;
std::unique_ptr<IgotPltSection> igotPlt;
std::unique_ptr<SyntheticSection> armCmseSGSection;
std::unique_ptr<PPC64LongBranchTargetSection> ppc64LongBranchTarget;
std::unique_ptr<SyntheticSection> mipsAbiFlags;
std::unique_ptr<MipsGotSection> mipsGot;
Expand Down
3 changes: 0 additions & 3 deletions lld/ELF/Target.h
Expand Up @@ -200,8 +200,6 @@ static inline std::string getErrorLocation(const uint8_t *loc) {
return getErrorPlace(loc).loc;
}

void processArmCmseSymbols();

void writePPC32GlinkSection(uint8_t *buf, size_t numEntries);

unsigned getPPCDFormOp(unsigned secondaryOp);
Expand All @@ -223,7 +221,6 @@ void writePrefixedInstruction(uint8_t *loc, uint64_t insn);
void addPPC64SaveRestore();
uint64_t getPPC64TocBase();
uint64_t getAArch64Page(uint64_t expr);
template <typename ELFT> void writeARMCmseImportLib();
void riscvFinalizeRelax(int passes);
void mergeRISCVAttributesSections();

Expand Down
10 changes: 0 additions & 10 deletions lld/ELF/Writer.cpp
Expand Up @@ -454,11 +454,6 @@ template <class ELFT> void elf::createSyntheticSections() {
in.igotPlt = std::make_unique<IgotPltSection>();
add(*in.igotPlt);

if (config->emachine == EM_ARM) {
in.armCmseSGSection = std::make_unique<ArmCmseSGSection>();
add(*in.armCmseSGSection);
}

// _GLOBAL_OFFSET_TABLE_ is defined relative to either .got.plt or .got. Treat
// it as a relocation and ensure the referenced section is created.
if (ElfSym::globalOffsetTable && config->emachine != EM_MIPS) {
Expand Down Expand Up @@ -600,9 +595,6 @@ template <class ELFT> void Writer<ELFT>::run() {
if (auto e = buffer->commit())
fatal("failed to write output '" + buffer->getPath() +
"': " + toString(std::move(e)));

if (!config->cmseOutputLib.empty())
writeARMCmseImportLib<ELFT>();
}
}

Expand Down Expand Up @@ -1991,7 +1983,6 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {

removeUnusedSyntheticSections();
script->diagnoseOrphanHandling();
script->diagnoseMissingSGSectionAddress();

sortSections();

Expand Down Expand Up @@ -2143,7 +2134,6 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {
// static symbol table.
finalizeSynthetic(in.symTab.get());
finalizeSynthetic(in.ppc64LongBranchTarget.get());
finalizeSynthetic(in.armCmseSGSection.get());
}

// Relaxation to delete inter-basic block jumps created by basic block
Expand Down

0 comments on commit cd116e0

Please sign in to comment.