diff options
author | Dimitry Andric <dim@FreeBSD.org> | 2024-01-24 19:17:23 +0000 |
---|---|---|
committer | Dimitry Andric <dim@FreeBSD.org> | 2024-04-19 21:24:44 +0000 |
commit | ab50317e96e57dee5b3ff4ad3f16f205b2a3359e (patch) | |
tree | 4b1f388eb6a07e574417aaacecd3ec4a83550718 /contrib/llvm-project/llvm/lib/Object | |
parent | 412542983a5ba62902141a8a7e155cceb9196a66 (diff) |
Diffstat (limited to 'contrib/llvm-project/llvm/lib/Object')
9 files changed, 116 insertions, 41 deletions
diff --git a/contrib/llvm-project/llvm/lib/Object/Archive.cpp b/contrib/llvm-project/llvm/lib/Object/Archive.cpp index 4ac4d727afb6..e447e5b23316 100644 --- a/contrib/llvm-project/llvm/lib/Object/Archive.cpp +++ b/contrib/llvm-project/llvm/lib/Object/Archive.cpp @@ -567,7 +567,7 @@ Expected<std::string> Archive::Child::getFullName() const { SmallString<128> FullName = sys::path::parent_path( Parent->getMemoryBufferRef().getBufferIdentifier()); sys::path::append(FullName, Name); - return std::string(FullName.str()); + return std::string(FullName); } Expected<StringRef> Archive::Child::getBuffer() const { diff --git a/contrib/llvm-project/llvm/lib/Object/ArchiveWriter.cpp b/contrib/llvm-project/llvm/lib/Object/ArchiveWriter.cpp index 2f70c9edd13e..155926a8c594 100644 --- a/contrib/llvm-project/llvm/lib/Object/ArchiveWriter.cpp +++ b/contrib/llvm-project/llvm/lib/Object/ArchiveWriter.cpp @@ -950,7 +950,7 @@ Expected<std::string> computeArchiveRelativePath(StringRef From, StringRef To) { for (auto ToE = sys::path::end(PathTo); ToI != ToE; ++ToI) sys::path::append(Relative, sys::path::Style::posix, *ToI); - return std::string(Relative.str()); + return std::string(Relative); } static Error writeArchiveToStream(raw_ostream &Out, diff --git a/contrib/llvm-project/llvm/lib/Object/Binary.cpp b/contrib/llvm-project/llvm/lib/Object/Binary.cpp index 0b9d95485287..2dfae8ab5d3c 100644 --- a/contrib/llvm-project/llvm/lib/Object/Binary.cpp +++ b/contrib/llvm-project/llvm/lib/Object/Binary.cpp @@ -84,6 +84,7 @@ Expected<std::unique_ptr<Binary>> object::createBinary(MemoryBufferRef Buffer, // PDB does not support the Binary interface. return errorCodeToError(object_error::invalid_file_type); case file_magic::unknown: + case file_magic::clang_ast: case file_magic::cuda_fatbinary: case file_magic::coff_cl_gl_object: case file_magic::dxcontainer_object: diff --git a/contrib/llvm-project/llvm/lib/Object/COFFImportFile.cpp b/contrib/llvm-project/llvm/lib/Object/COFFImportFile.cpp index eeb13ffe9c11..60556c149bf7 100644 --- a/contrib/llvm-project/llvm/lib/Object/COFFImportFile.cpp +++ b/contrib/llvm-project/llvm/lib/Object/COFFImportFile.cpp @@ -33,6 +33,25 @@ using namespace llvm; namespace llvm { namespace object { +StringRef COFFImportFile::getFileFormatName() const { + switch (getMachine()) { + case COFF::IMAGE_FILE_MACHINE_I386: + return "COFF-import-file-i386"; + case COFF::IMAGE_FILE_MACHINE_AMD64: + return "COFF-import-file-x86-64"; + case COFF::IMAGE_FILE_MACHINE_ARMNT: + return "COFF-import-file-ARM"; + case COFF::IMAGE_FILE_MACHINE_ARM64: + return "COFF-import-file-ARM64"; + case COFF::IMAGE_FILE_MACHINE_ARM64EC: + return "COFF-import-file-ARM64EC"; + case COFF::IMAGE_FILE_MACHINE_ARM64X: + return "COFF-import-file-ARM64X"; + default: + return "COFF-import-file-<unknown arch>"; + } +} + static uint16_t getImgRelRelocation(MachineTypes Machine) { switch (Machine) { default: @@ -131,7 +150,7 @@ namespace { class ObjectFactory { using u16 = support::ulittle16_t; using u32 = support::ulittle32_t; - MachineTypes Machine; + MachineTypes NativeMachine; BumpPtrAllocator Alloc; StringRef ImportName; StringRef Library; @@ -140,7 +159,7 @@ class ObjectFactory { public: ObjectFactory(StringRef S, MachineTypes M) - : Machine(M), ImportName(S), Library(llvm::sys::path::stem(S)), + : NativeMachine(M), ImportName(S), Library(llvm::sys::path::stem(S)), ImportDescriptorSymbolName(("__IMPORT_DESCRIPTOR_" + Library).str()), NullThunkSymbolName(("\x7f" + Library + "_NULL_THUNK_DATA").str()) {} @@ -163,10 +182,14 @@ public: // Create a short import file which is described in PE/COFF spec 7. Import // Library Format. NewArchiveMember createShortImport(StringRef Sym, uint16_t Ordinal, - ImportType Type, ImportNameType NameType); + ImportType Type, ImportNameType NameType, + MachineTypes Machine); // Create a weak external file which is described in PE/COFF Aux Format 3. - NewArchiveMember createWeakExternal(StringRef Sym, StringRef Weak, bool Imp); + NewArchiveMember createWeakExternal(StringRef Sym, StringRef Weak, bool Imp, + MachineTypes Machine); + + bool is64Bit() const { return COFF::is64Bit(NativeMachine); } }; } // namespace @@ -178,7 +201,7 @@ ObjectFactory::createImportDescriptor(std::vector<uint8_t> &Buffer) { // COFF Header coff_file_header Header{ - u16(Machine), + u16(NativeMachine), u16(NumberOfSections), u32(0), u32(sizeof(Header) + (NumberOfSections * sizeof(coff_section)) + @@ -189,7 +212,7 @@ ObjectFactory::createImportDescriptor(std::vector<uint8_t> &Buffer) { (ImportName.size() + 1)), u32(NumberOfSymbols), u16(0), - u16(is64Bit(Machine) ? C_Invalid : IMAGE_FILE_32BIT_MACHINE), + u16(is64Bit() ? C_Invalid : IMAGE_FILE_32BIT_MACHINE), }; append(Buffer, Header); @@ -231,11 +254,11 @@ ObjectFactory::createImportDescriptor(std::vector<uint8_t> &Buffer) { const coff_relocation RelocationTable[NumberOfRelocations] = { {u32(offsetof(coff_import_directory_table_entry, NameRVA)), u32(2), - u16(getImgRelRelocation(Machine))}, + u16(getImgRelRelocation(NativeMachine))}, {u32(offsetof(coff_import_directory_table_entry, ImportLookupTableRVA)), - u32(3), u16(getImgRelRelocation(Machine))}, + u32(3), u16(getImgRelRelocation(NativeMachine))}, {u32(offsetof(coff_import_directory_table_entry, ImportAddressTableRVA)), - u32(4), u16(getImgRelRelocation(Machine))}, + u32(4), u16(getImgRelRelocation(NativeMachine))}, }; append(Buffer, RelocationTable); @@ -317,7 +340,7 @@ ObjectFactory::createNullImportDescriptor(std::vector<uint8_t> &Buffer) { // COFF Header coff_file_header Header{ - u16(Machine), + u16(NativeMachine), u16(NumberOfSections), u32(0), u32(sizeof(Header) + (NumberOfSections * sizeof(coff_section)) + @@ -325,7 +348,7 @@ ObjectFactory::createNullImportDescriptor(std::vector<uint8_t> &Buffer) { sizeof(coff_import_directory_table_entry)), u32(NumberOfSymbols), u16(0), - u16(is64Bit(Machine) ? C_Invalid : IMAGE_FILE_32BIT_MACHINE), + u16(is64Bit() ? C_Invalid : IMAGE_FILE_32BIT_MACHINE), }; append(Buffer, Header); @@ -374,11 +397,11 @@ ObjectFactory::createNullImportDescriptor(std::vector<uint8_t> &Buffer) { NewArchiveMember ObjectFactory::createNullThunk(std::vector<uint8_t> &Buffer) { const uint32_t NumberOfSections = 2; const uint32_t NumberOfSymbols = 1; - uint32_t VASize = is64Bit(Machine) ? 8 : 4; + uint32_t VASize = is64Bit() ? 8 : 4; // COFF Header coff_file_header Header{ - u16(Machine), + u16(NativeMachine), u16(NumberOfSections), u32(0), u32(sizeof(Header) + (NumberOfSections * sizeof(coff_section)) + @@ -388,7 +411,7 @@ NewArchiveMember ObjectFactory::createNullThunk(std::vector<uint8_t> &Buffer) { VASize), u32(NumberOfSymbols), u16(0), - u16(is64Bit(Machine) ? C_Invalid : IMAGE_FILE_32BIT_MACHINE), + u16(is64Bit() ? C_Invalid : IMAGE_FILE_32BIT_MACHINE), }; append(Buffer, Header); @@ -403,8 +426,7 @@ NewArchiveMember ObjectFactory::createNullThunk(std::vector<uint8_t> &Buffer) { u32(0), u16(0), u16(0), - u32((is64Bit(Machine) ? IMAGE_SCN_ALIGN_8BYTES - : IMAGE_SCN_ALIGN_4BYTES) | + u32((is64Bit() ? IMAGE_SCN_ALIGN_8BYTES : IMAGE_SCN_ALIGN_4BYTES) | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE)}, {{'.', 'i', 'd', 'a', 't', 'a', '$', '4'}, @@ -417,8 +439,7 @@ NewArchiveMember ObjectFactory::createNullThunk(std::vector<uint8_t> &Buffer) { u32(0), u16(0), u16(0), - u32((is64Bit(Machine) ? IMAGE_SCN_ALIGN_8BYTES - : IMAGE_SCN_ALIGN_4BYTES) | + u32((is64Bit() ? IMAGE_SCN_ALIGN_8BYTES : IMAGE_SCN_ALIGN_4BYTES) | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE)}, }; @@ -426,12 +447,12 @@ NewArchiveMember ObjectFactory::createNullThunk(std::vector<uint8_t> &Buffer) { // .idata$5, ILT append(Buffer, u32(0)); - if (is64Bit(Machine)) + if (is64Bit()) append(Buffer, u32(0)); // .idata$4, IAT append(Buffer, u32(0)); - if (is64Bit(Machine)) + if (is64Bit()) append(Buffer, u32(0)); // Symbol Table @@ -456,7 +477,8 @@ NewArchiveMember ObjectFactory::createNullThunk(std::vector<uint8_t> &Buffer) { NewArchiveMember ObjectFactory::createShortImport(StringRef Sym, uint16_t Ordinal, ImportType ImportType, - ImportNameType NameType) { + ImportNameType NameType, + MachineTypes Machine) { size_t ImpSize = ImportName.size() + Sym.size() + 2; // +2 for NULs size_t Size = sizeof(coff_import_header) + ImpSize; char *Buf = Alloc.Allocate<char>(Size); @@ -482,7 +504,8 @@ NewArchiveMember ObjectFactory::createShortImport(StringRef Sym, } NewArchiveMember ObjectFactory::createWeakExternal(StringRef Sym, - StringRef Weak, bool Imp) { + StringRef Weak, bool Imp, + MachineTypes Machine) { std::vector<uint8_t> Buffer; const uint32_t NumberOfSections = 1; const uint32_t NumberOfSymbols = 5; @@ -566,8 +589,11 @@ Error writeImportLibrary(StringRef ImportName, StringRef Path, ArrayRef<COFFShortExport> Exports, MachineTypes Machine, bool MinGW) { + MachineTypes NativeMachine = + isArm64EC(Machine) ? IMAGE_FILE_MACHINE_ARM64 : Machine; + std::vector<NewArchiveMember> Members; - ObjectFactory OF(llvm::sys::path::filename(ImportName), Machine); + ObjectFactory OF(llvm::sys::path::filename(ImportName), NativeMachine); std::vector<uint8_t> ImportDescriptor; Members.push_back(OF.createImportDescriptor(ImportDescriptor)); @@ -601,13 +627,15 @@ Error writeImportLibrary(StringRef ImportName, StringRef Path, return Name.takeError(); if (!E.AliasTarget.empty() && *Name != E.AliasTarget) { - Members.push_back(OF.createWeakExternal(E.AliasTarget, *Name, false)); - Members.push_back(OF.createWeakExternal(E.AliasTarget, *Name, true)); + Members.push_back( + OF.createWeakExternal(E.AliasTarget, *Name, false, Machine)); + Members.push_back( + OF.createWeakExternal(E.AliasTarget, *Name, true, Machine)); continue; } Members.push_back( - OF.createShortImport(*Name, E.Ordinal, ImportType, NameType)); + OF.createShortImport(*Name, E.Ordinal, ImportType, NameType, Machine)); } return writeArchive(Path, Members, SymtabWritingMode::NormalSymtab, diff --git a/contrib/llvm-project/llvm/lib/Object/ELFObjectFile.cpp b/contrib/llvm-project/llvm/lib/Object/ELFObjectFile.cpp index ae21b81c10c8..28b96c341e3f 100644 --- a/contrib/llvm-project/llvm/lib/Object/ELFObjectFile.cpp +++ b/contrib/llvm-project/llvm/lib/Object/ELFObjectFile.cpp @@ -830,6 +830,10 @@ Expected<std::vector<BBAddrMap>> static readBBAddrMapImpl( std::move(BBAddrMapOrErr->begin(), BBAddrMapOrErr->end(), std::back_inserter(BBAddrMaps)); } + if (PGOAnalyses) + assert(PGOAnalyses->size() == BBAddrMaps.size() && + "The same number of BBAddrMaps and PGOAnalysisMaps should be " + "returned when PGO information is requested"); return BBAddrMaps; } diff --git a/contrib/llvm-project/llvm/lib/Object/ObjectFile.cpp b/contrib/llvm-project/llvm/lib/Object/ObjectFile.cpp index c05eb0a0468e..6a226a3bbdbc 100644 --- a/contrib/llvm-project/llvm/lib/Object/ObjectFile.cpp +++ b/contrib/llvm-project/llvm/lib/Object/ObjectFile.cpp @@ -155,6 +155,7 @@ ObjectFile::createObjectFile(MemoryBufferRef Object, file_magic Type, switch (Type) { case file_magic::unknown: case file_magic::bitcode: + case file_magic::clang_ast: case file_magic::coff_cl_gl_object: case file_magic::archive: case file_magic::macho_universal_binary: diff --git a/contrib/llvm-project/llvm/lib/Object/OffloadBinary.cpp b/contrib/llvm-project/llvm/lib/Object/OffloadBinary.cpp index 1de784c44da1..bfc35e41fe65 100644 --- a/contrib/llvm-project/llvm/lib/Object/OffloadBinary.cpp +++ b/contrib/llvm-project/llvm/lib/Object/OffloadBinary.cpp @@ -343,3 +343,35 @@ StringRef object::getImageKindName(ImageKind Kind) { return ""; } } + +bool object::areTargetsCompatible(const OffloadFile::TargetID &LHS, + const OffloadFile::TargetID &RHS) { + // Exact matches are not considered compatible because they are the same + // target. We are interested in different targets that are compatible. + if (LHS == RHS) + return false; + + // The triples must match at all times. + if (LHS.first != RHS.first) + return false; + + // Only The AMDGPU target requires additional checks. + llvm::Triple T(LHS.first); + if (!T.isAMDGPU()) + return false; + + // The base processor must always match. + if (LHS.second.split(":").first != RHS.second.split(":").first) + return false; + + // Check combintions of on / off features that must match. + if (LHS.second.contains("xnack+") && RHS.second.contains("xnack-")) + return false; + if (LHS.second.contains("xnack-") && RHS.second.contains("xnack+")) + return false; + if (LHS.second.contains("sramecc-") && RHS.second.contains("sramecc+")) + return false; + if (LHS.second.contains("sramecc+") && RHS.second.contains("sramecc-")) + return false; + return true; +} diff --git a/contrib/llvm-project/llvm/lib/Object/RelocationResolver.cpp b/contrib/llvm-project/llvm/lib/Object/RelocationResolver.cpp index ae97107f67fa..564d9da78e97 100644 --- a/contrib/llvm-project/llvm/lib/Object/RelocationResolver.cpp +++ b/contrib/llvm-project/llvm/lib/Object/RelocationResolver.cpp @@ -538,6 +538,8 @@ static bool supportsLoongArch(uint64_t Type) { case ELF::R_LARCH_32: case ELF::R_LARCH_32_PCREL: case ELF::R_LARCH_64: + case ELF::R_LARCH_ADD6: + case ELF::R_LARCH_SUB6: case ELF::R_LARCH_ADD8: case ELF::R_LARCH_SUB8: case ELF::R_LARCH_ADD16: @@ -563,6 +565,10 @@ static uint64_t resolveLoongArch(uint64_t Type, uint64_t Offset, uint64_t S, return (S + Addend - Offset) & 0xFFFFFFFF; case ELF::R_LARCH_64: return S + Addend; + case ELF::R_LARCH_ADD6: + return (LocData & 0xC0) | ((LocData + S + Addend) & 0x3F); + case ELF::R_LARCH_SUB6: + return (LocData & 0xC0) | ((LocData - (S + Addend)) & 0x3F); case ELF::R_LARCH_ADD8: return (LocData + (S + Addend)) & 0xFF; case ELF::R_LARCH_SUB8: @@ -879,8 +885,10 @@ uint64_t resolveRelocation(RelocationResolver Resolver, const RelocationRef &R, if (GetRelSectionType() == ELF::SHT_RELA) { Addend = getELFAddend(R); - // RISCV relocations use both LocData and Addend. - if (Obj->getArch() != Triple::riscv32 && + // LoongArch and RISCV relocations use both LocData and Addend. + if (Obj->getArch() != Triple::loongarch32 && + Obj->getArch() != Triple::loongarch64 && + Obj->getArch() != Triple::riscv32 && Obj->getArch() != Triple::riscv64) LocData = 0; } diff --git a/contrib/llvm-project/llvm/lib/Object/WasmObjectFile.cpp b/contrib/llvm-project/llvm/lib/Object/WasmObjectFile.cpp index 94cd96968ff2..b9a8e970216b 100644 --- a/contrib/llvm-project/llvm/lib/Object/WasmObjectFile.cpp +++ b/contrib/llvm-project/llvm/lib/Object/WasmObjectFile.cpp @@ -258,7 +258,7 @@ static wasm::WasmLimits readLimits(WasmObjectFile::ReadContext &Ctx) { static wasm::WasmTableType readTableType(WasmObjectFile::ReadContext &Ctx) { wasm::WasmTableType TableType; - TableType.ElemType = readUint8(Ctx); + TableType.ElemType = wasm::ValType(readVaruint32(Ctx)); TableType.Limits = readLimits(Ctx); return TableType; } @@ -1163,8 +1163,8 @@ Error WasmObjectFile::parseImportSection(ReadContext &Ctx) { Im.Table = readTableType(Ctx); NumImportedTables++; auto ElemType = Im.Table.ElemType; - if (ElemType != wasm::WASM_TYPE_FUNCREF && - ElemType != wasm::WASM_TYPE_EXTERNREF) + if (ElemType != wasm::ValType::FUNCREF && + ElemType != wasm::ValType::EXTERNREF) return make_error<GenericBinaryError>("invalid table element type", object_error::parse_failed); break; @@ -1220,8 +1220,8 @@ Error WasmObjectFile::parseTableSection(ReadContext &Ctx) { T.Index = NumImportedTables + Tables.size(); Tables.push_back(T); auto ElemType = Tables.back().Type.ElemType; - if (ElemType != wasm::WASM_TYPE_FUNCREF && - ElemType != wasm::WASM_TYPE_EXTERNREF) { + if (ElemType != wasm::ValType::FUNCREF && + ElemType != wasm::ValType::EXTERNREF) { return make_error<GenericBinaryError>("invalid table element type", object_error::parse_failed); } @@ -1534,21 +1534,22 @@ Error WasmObjectFile::parseElemSection(ReadContext &Ctx) { } if (Segment.Flags & wasm::WASM_ELEM_SEGMENT_MASK_HAS_ELEM_KIND) { - Segment.ElemKind = readUint8(Ctx); + auto ElemKind = readVaruint32(Ctx); if (Segment.Flags & wasm::WASM_ELEM_SEGMENT_HAS_INIT_EXPRS) { - if (Segment.ElemKind != uint8_t(wasm::ValType::FUNCREF) && - Segment.ElemKind != uint8_t(wasm::ValType::EXTERNREF)) { + Segment.ElemKind = wasm::ValType(ElemKind); + if (Segment.ElemKind != wasm::ValType::FUNCREF && + Segment.ElemKind != wasm::ValType::EXTERNREF) { return make_error<GenericBinaryError>("invalid reference type", object_error::parse_failed); } } else { - if (Segment.ElemKind != 0) + if (ElemKind != 0) return make_error<GenericBinaryError>("invalid elemtype", object_error::parse_failed); - Segment.ElemKind = uint8_t(wasm::ValType::FUNCREF); + Segment.ElemKind = wasm::ValType::FUNCREF; } } else { - Segment.ElemKind = uint8_t(wasm::ValType::FUNCREF); + Segment.ElemKind = wasm::ValType::FUNCREF; } if (Segment.Flags & wasm::WASM_ELEM_SEGMENT_HAS_INIT_EXPRS) |