Index: lld/COFF/Chunks.h =================================================================== --- lld/COFF/Chunks.h +++ lld/COFF/Chunks.h @@ -695,6 +695,27 @@ void writeTo(uint8_t *buf) const override {} }; +class ECCodeMapEntry { +public: + ECCodeMapEntry(Chunk *first, Chunk *last, chpe_range_type type) + : first(first), last(last), type(type) {} + Chunk *first; + Chunk *last; + chpe_range_type type; +}; + +// This is a chunk containing CHPE code map on EC targets. It's a table +// of address ranges and their types. +class ECCodeMapChunk : public NonSectionChunk { +public: + ECCodeMapChunk(std::vector &map) : map(map) {} + size_t getSize() const override; + void writeTo(uint8_t *buf) const override; + +private: + std::vector ↦ +}; + // MinGW specific, for the "automatic import of variables from DLLs" feature. // This provides the table of runtime pseudo relocations, for variable // references that turned out to need to be imported from a DLL even though Index: lld/COFF/Chunks.cpp =================================================================== --- lld/COFF/Chunks.cpp +++ lld/COFF/Chunks.cpp @@ -896,6 +896,20 @@ "RVA tables should be de-duplicated"); } +size_t ECCodeMapChunk::getSize() const { + return map.size() * sizeof(chpe_range_entry); +} + +void ECCodeMapChunk::writeTo(uint8_t *buf) const { + auto table = reinterpret_cast(buf); + for (uint32_t i = 0; i < map.size(); i++) { + const ECCodeMapEntry &entry = map[i]; + uint32_t start = entry.first->getRVA(); + table[i].StartOffset = start | entry.type; + table[i].Length = entry.last->getRVA() + entry.last->getSize() - start; + } +} + // MinGW specific, for the "automatic import of variables from DLLs" feature. size_t PseudoRelocTableChunk::getSize() const { if (relocs.empty()) Index: lld/COFF/Driver.cpp =================================================================== --- lld/COFF/Driver.cpp +++ lld/COFF/Driver.cpp @@ -2277,6 +2277,11 @@ ctx.symtab.addAbsolute(mangle("__guard_eh_cont_count"), 0); ctx.symtab.addAbsolute(mangle("__guard_eh_cont_table"), 0); + if (COFF::isArm64EC(config->machine)) { + ctx.symtab.addAbsolute("__hybrid_code_map", 0); + ctx.symtab.addAbsolute("__hybrid_code_map_count", 0); + } + if (config->pseudoRelocs) { ctx.symtab.addAbsolute(mangle("__RUNTIME_PSEUDO_RELOC_LIST__"), 0); ctx.symtab.addAbsolute(mangle("__RUNTIME_PSEUDO_RELOC_LIST_END__"), 0); Index: lld/COFF/Writer.cpp =================================================================== --- lld/COFF/Writer.cpp +++ lld/COFF/Writer.cpp @@ -220,6 +220,7 @@ uint16_t type, int margin); bool createThunks(OutputSection *os, int margin); bool verifyRanges(const std::vector chunks); + void createECCodeMap(); void finalizeAddresses(); void removeEmptySections(); void assignOutputSectionIndices(); @@ -228,6 +229,7 @@ template void writeHeader(); void createSEHTable(); void createRuntimePseudoRelocs(); + void createECChunks(); void insertCtorDtorSymbols(); void markSymbolsWithRelocations(ObjFile *file, SymbolRVASet &usedSymbols); void createGuardCFTables(); @@ -270,6 +272,7 @@ std::map partialSections; std::vector strtab; std::vector outputSymtab; + std::vector codeMap; IdataContents idata; Chunk *importTableStart = nullptr; uint64_t importTableSize = 0; @@ -524,6 +527,55 @@ return addressesChanged; } +// Create a code map for CHPE metadata. +void Writer::createECCodeMap() { + if (!isArm64EC(ctx.config.machine)) + return; + + // Clear the map in case we were're recomputing the map after adding + // a range extension thunk. + codeMap.clear(); + + Chunk *first = nullptr, *last; + chpe_range_type lastType; + + auto closeRange = [&]() { + if (first) { + codeMap.push_back({first, last, lastType}); + first = nullptr; + } + }; + + for (OutputSection *sec : ctx.outputSections) { + if (!sec->isCodeSection()) { + closeRange(); + continue; + } + + for (Chunk *c : sec->chunks) { + // Skip empty section chunks. MSVC does not seem to do that and + // generates empty code ranges in some cases. + if (isa(c) && !c->getSize()) + continue; + + chpe_range_type chunkType = c->getECRangeType(); + if (!first) { + first = c; + } else if (lastType != chunkType) { + closeRange(); + first = c; + } + last = c; + lastType = chunkType; + } + } + + closeRange(); + + Symbol *tableCountSym = ctx.symtab.findUnderscore("__hybrid_code_map_count"); + cast(tableCountSym)->setVA(codeMap.size()); +} + // Verify that all relocations are in range, with no extra margin requirements. bool Writer::verifyRanges(const std::vector chunks) { for (Chunk *c : chunks) { @@ -1058,6 +1110,9 @@ if (config->guardCF != GuardCFLevel::Off) createGuardCFTables(); + if (isArm64EC(config->machine)) + createECChunks(); + if (config->autoImport) createRuntimePseudoRelocs(); @@ -1372,6 +1427,10 @@ void Writer::assignAddresses() { Configuration *config = &ctx.config; + // We need to create EC code map so that ECCodeMapChunk knows its size. + // We do it here to make sure that we account for range extension chunks. + createECCodeMap(); + sizeOfHeaders = dosStubSize + sizeof(PEMagic) + sizeof(coff_file_header) + sizeof(data_directory) * numberOfDataDirectory + sizeof(coff_section) * ctx.outputSections.size(); @@ -1908,6 +1967,15 @@ cast(c)->setVA(tableChunk->getSize() / (hasFlag ? 5 : 4)); } +// Create CHPE metadata chunks. +void Writer::createECChunks() { + auto codeMapChunk = make(codeMap); + rdataSec->addChunk(codeMapChunk); + Symbol *codeMapSym = ctx.symtab.findUnderscore("__hybrid_code_map"); + replaceSymbol(codeMapSym, codeMapSym->getName(), + codeMapChunk); +} + // MinGW specific. Gather all relocations that are imported from a DLL even // though the code didn't expect it to, produce the table that the runtime // uses for fixing them up, and provide the synthetic symbols that the Index: lld/test/COFF/Inputs/loadconfig-arm64ec.s =================================================================== --- lld/test/COFF/Inputs/loadconfig-arm64ec.s +++ lld/test/COFF/Inputs/loadconfig-arm64ec.s @@ -46,8 +46,8 @@ .p2align 2, 0 __chpe_metadata: .word 1 - .rva code_map - .word code_map_count + .rva __hybrid_code_map + .word __hybrid_code_map_count .word 0 // __x64_code_ranges_to_entry_points .word 0 //__arm64x_redirection_metadata .rva __os_arm64x_dispatch_call_no_redirect Index: lld/test/COFF/arm64ec-codemap.test =================================================================== --- lld/test/COFF/arm64ec-codemap.test +++ lld/test/COFF/arm64ec-codemap.test @@ -3,16 +3,15 @@ RUN: llvm-mc -filetype=obj -triple=arm64-windows arm64-func-sym.s -o arm64-func-sym.obj RUN: llvm-mc -filetype=obj -triple=arm64ec-windows arm64ec-func-sym.s -o arm64ec-func-sym.obj +RUN: llvm-mc -filetype=obj -triple=arm64ec-windows empty-sec.s -o arm64ec-empty-sec.obj RUN: llvm-mc -filetype=obj -triple=x86_64-windows x86_64-func-sym.s -o x86_64-func-sym.obj -RUN: llvm-mc -filetype=obj -triple=arm64ec-windows codemap.s -o codemap.obj -RUN: llvm-mc -filetype=obj -triple=arm64ec-windows codemap2.s -o codemap2.obj -RUN: llvm-mc -filetype=obj -triple=arm64ec-windows codemap3.s -o codemap3.obj +RUN: llvm-mc -filetype=obj -triple=x86_64-windows empty-sec.s -o x86_64-empty-sec.obj RUN: llvm-mc -filetype=obj -triple=arm64ec-windows %S/Inputs/loadconfig-arm64ec.s -o loadconfig-arm64ec.obj Link ARM64EC DLL and verify that the code is arranged as expected. RUN: lld-link -out:test.dll -machine:arm64ec arm64ec-func-sym.obj x86_64-func-sym.obj \ -RUN: codemap.obj loadconfig-arm64ec.obj -dll -noentry +RUN: loadconfig-arm64ec.obj -dll -noentry RUN: llvm-readobj --coff-load-config test.dll | FileCheck -check-prefix=CODEMAP %s CODEMAP: CodeMap [ @@ -44,12 +43,18 @@ Order of arguments doesn't matter in this case, chunks are sorted by target type anyway. RUN: lld-link -out:test2.dll -machine:arm64ec x86_64-func-sym.obj arm64ec-func-sym.obj \ -RUN: codemap.obj loadconfig-arm64ec.obj -dll -noentry +RUN: loadconfig-arm64ec.obj -dll -noentry RUN: llvm-readobj --coff-load-config test2.dll | FileCheck -check-prefix=CODEMAP %s RUN: llvm-objdump -d test2.dll | FileCheck -check-prefix=DISASM %s RUN: lld-link -out:testx.dll -machine:arm64x arm64-func-sym.obj arm64ec-func-sym.obj \ -RUN: x86_64-func-sym.obj codemap2.obj loadconfig-arm64ec.obj -dll -noentry +RUN: x86_64-func-sym.obj loadconfig-arm64ec.obj -dll -noentry + +Adding empty chunks does not affect code map ranges. + +RUN: lld-link -out:test3.dll -machine:arm64ec x86_64-empty-sec.obj arm64ec-empty-sec.obj \ +RUN: arm64ec-func-sym.obj x86_64-func-sym.obj loadconfig-arm64ec.obj -dll -noentry +RUN: llvm-readobj --coff-load-config test3.dll | FileCheck -check-prefix=CODEMAP %s Do the same with ARM64X target. @@ -87,7 +92,7 @@ Test merged sections. RUN: lld-link -out:testm.dll -machine:arm64ec arm64ec-func-sym.obj x86_64-func-sym.obj \ -RUN: codemap3.obj loadconfig-arm64ec.obj -dll -noentry -merge:test=.text +RUN: loadconfig-arm64ec.obj -dll -noentry -merge:test=.text RUN: llvm-readobj --coff-load-config testm.dll | FileCheck -check-prefix=CODEMAPM %s CODEMAPM: CodeMap [ @@ -148,48 +153,7 @@ movl $6, %eax retq -#--- codemap.s - .section .rdata,"dr" - .globl code_map -code_map: - .rva arm64ec_func_sym + 1 - .word 8 - .rva x86_64_func_sym + 2 - .word 6 - .rva arm64ec_func_sym2 + 1 - .word 8 - .rva x86_64_func_sym2 + 2 - .word 6 - - .globl code_map_count -code_map_count = 4 - -#--- codemap2.s - .section .rdata,"dr" - .globl code_map -code_map: - .rva arm64_func_sym - .word 8 - .rva arm64ec_func_sym + 1 - .word 8 - .rva x86_64_func_sym + 2 - .word 6 - .rva arm64ec_func_sym2 + 1 - .word 8 - .rva x86_64_func_sym2 + 2 - .word 6 - - .globl code_map_count -code_map_count = 5 - -#--- codemap3.s - .section .rdata,"dr" - .globl code_map -code_map: - .rva arm64ec_func_sym + 1 - .word 16 - .rva x86_64_func_sym + 2 - .word 14 - - .globl code_map_count -code_map_count = 2 +#--- empty-sec.s + .section .empty1, "xr" + .section .empty2, "xr" + .section .empty3, "xr"