Index: llvm/docs/LinkTimeOptimization.rst =================================================================== --- llvm/docs/LinkTimeOptimization.rst +++ llvm/docs/LinkTimeOptimization.rst @@ -249,6 +249,12 @@ The attributes of a symbol include the alignment, visibility, and kind. +Tools working with object files on Darwin (e.g. lipo) may need to know properties like the CPU type: + +.. code-block:: c + + lto_module_get_macho_cputype(lto_module_t mod, unsigned int *out_cputype, unsigned int *out_cpusubtype) + ``lto_code_gen_t`` ------------------ Index: llvm/include/llvm-c/lto.h =================================================================== --- llvm/include/llvm-c/lto.h +++ llvm/include/llvm-c/lto.h @@ -46,7 +46,7 @@ * @{ */ -#define LTO_API_VERSION 26 +#define LTO_API_VERSION 27 /** * \since prior to LTO_API_VERSION=3 @@ -297,6 +297,21 @@ extern const char* lto_module_get_linkeropts(lto_module_t mod); +/** + * If targeting mach-o on darwin, this function gets the CPU type and subtype + * that will end up being encoded in the mach-o header. These are the values + * that can be found in mach/machine.h. + * + * \p out_cputype and \p out_cpusubtype must be non-NULL. + * + * Returns true on error (check lto_get_error_message() for details). + * + * \since LTO_API_VERSION=27 + */ +extern lto_bool_t lto_module_get_macho_cputype(lto_module_t mod, + unsigned int *out_cputype, + unsigned int *out_cpusubtype); + /** * Diagnostic severity. * Index: llvm/include/llvm/LTO/legacy/LTOModule.h =================================================================== --- llvm/include/llvm/LTO/legacy/LTOModule.h +++ llvm/include/llvm/LTO/legacy/LTOModule.h @@ -165,6 +165,10 @@ static const char *getDependentLibrary(lto::InputFile *input, size_t index, size_t *size); + Expected getMachOCPUType() const; + + Expected getMachOCPUSubType() const; + private: /// Parse metadata from the module // FIXME: it only parses "llvm.linker.options" metadata at the moment Index: llvm/lib/LTO/LTOModule.cpp =================================================================== --- llvm/lib/LTO/LTOModule.cpp +++ llvm/lib/LTO/LTOModule.cpp @@ -28,6 +28,7 @@ #include "llvm/MC/MCSymbol.h" #include "llvm/MC/SubtargetFeature.h" #include "llvm/Object/IRObjectFile.h" +#include "llvm/Object/MachO.h" #include "llvm/Object/ObjectFile.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/Host.h" @@ -676,3 +677,11 @@ *size = S.size(); return S.data(); } + +Expected LTOModule::getMachOCPUType() const { + return MachO::getCPUType(Triple(Mod->getTargetTriple())); +} + +Expected LTOModule::getMachOCPUSubType() const { + return MachO::getCPUSubType(Triple(Mod->getTargetTriple())); +} Index: llvm/tools/lto/lto.cpp =================================================================== --- llvm/tools/lto/lto.cpp +++ llvm/tools/lto/lto.cpp @@ -327,6 +327,27 @@ return unwrap(mod)->getLinkerOpts().data(); } +lto_bool_t lto_module_get_macho_cputype(lto_module_t mod, + unsigned int *out_cputype, + unsigned int *out_cpusubtype) { + LTOModule *M = unwrap(mod); + Expected CPUType = M->getMachOCPUType(); + if (!CPUType) { + sLastErrorString = toString(CPUType.takeError()); + return true; + } + *out_cputype = *CPUType; + + Expected CPUSubType = M->getMachOCPUSubType(); + if (!CPUSubType) { + sLastErrorString = toString(CPUSubType.takeError()); + return true; + } + *out_cpusubtype = *CPUSubType; + + return false; +} + void lto_codegen_set_diagnostic_handler(lto_code_gen_t cg, lto_diagnostic_handler_t diag_handler, void *ctxt) { Index: llvm/tools/lto/lto.exports =================================================================== --- llvm/tools/lto/lto.exports +++ llvm/tools/lto/lto.exports @@ -9,6 +9,7 @@ lto_module_create_in_local_context lto_module_create_in_codegen_context lto_module_get_linkeropts +lto_module_get_macho_cputype lto_module_get_num_symbols lto_module_get_symbol_attribute lto_module_get_symbol_name