diff --git a/llvm/include/llvm/BinaryFormat/Magic.h b/llvm/include/llvm/BinaryFormat/Magic.h --- a/llvm/include/llvm/BinaryFormat/Magic.h +++ b/llvm/include/llvm/BinaryFormat/Magic.h @@ -42,19 +42,20 @@ macho_universal_binary, ///< Mach-O universal binary macho_file_set, ///< Mach-O file set binary minidump, ///< Windows minidump file - coff_cl_gl_object, ///< Microsoft cl.exe's intermediate code file - coff_object, ///< COFF object file - coff_import_library, ///< COFF import library - pecoff_executable, ///< PECOFF executable file - windows_resource, ///< Windows compiled resource file (.res) - xcoff_object_32, ///< 32-bit XCOFF object file - xcoff_object_64, ///< 64-bit XCOFF object file - wasm_object, ///< WebAssembly Object file - pdb, ///< Windows PDB debug info file - tapi_file, ///< Text-based Dynamic Library Stub file - cuda_fatbinary, ///< CUDA Fatbinary object file - offload_binary, ///< LLVM offload object file - dxcontainer_object, ///< DirectX container file + coff_cl_gl_object, ///< Microsoft cl.exe's intermediate code file + coff_object, ///< COFF object file + coff_import_library, ///< COFF import library + pecoff_executable, ///< PECOFF executable file + windows_resource, ///< Windows compiled resource file (.res) + xcoff_object_32, ///< 32-bit XCOFF object file + xcoff_object_64, ///< 64-bit XCOFF object file + wasm_object, ///< WebAssembly Object file + pdb, ///< Windows PDB debug info file + tapi_file, ///< Text-based Dynamic Library Stub file + cuda_fatbinary, ///< CUDA Fatbinary object file + offload_binary, ///< LLVM offload object file + dxcontainer_object, ///< DirectX container file + aix_linker_import_file, ///< AIX linker import file }; bool is_object() const { return V != unknown; } diff --git a/llvm/include/llvm/Object/AIXLinkerImportFile.h b/llvm/include/llvm/Object/AIXLinkerImportFile.h new file mode 100644 --- /dev/null +++ b/llvm/include/llvm/Object/AIXLinkerImportFile.h @@ -0,0 +1,41 @@ +//===- AIXLinkerImportFile.h - AIX linker Import file implement-*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This file declares the AIX Linker Import File interface. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_AIX_LINKER_IMPORT_FILE_H +#define LLVM_AIX_LINKER_IMPORT_FILE_H + +#include "llvm/Object/Binary.h" +#include "llvm/Support/Error.h" +#include "llvm/Support/MemoryBufferRef.h" + +namespace llvm { +namespace object { + +// The class AIXLinkerImportFile, which is not a symbolic file at the moment. +// The command 'llvm-nm -export-symbols' exports an empty symbol for AIX linker +// import file that depends on the AIXLinkerImportFile. Furthermore, if we +// decide to get symbols from AIX linker import file and derive +// AIXLinkerImportFile from the SymbolicFile class, the llvm-nm tool will also +// need to be updated accordingly. + +class AIXLinkerImportFile : public Binary { +public: + AIXLinkerImportFile(unsigned int Type, MemoryBufferRef Source) + : Binary(Type, Source) {} + ~AIXLinkerImportFile(); + static Expected> + create(MemoryBufferRef Source); +}; +} // namespace object +} // namespace llvm + +#endif diff --git a/llvm/lib/BinaryFormat/Magic.cpp b/llvm/lib/BinaryFormat/Magic.cpp --- a/llvm/lib/BinaryFormat/Magic.cpp +++ b/llvm/lib/BinaryFormat/Magic.cpp @@ -31,7 +31,7 @@ /// Identify the magic in magic. file_magic llvm::identify_magic(StringRef Magic) { - if (Magic.size() < 4) + if (Magic.size() < 2) return file_magic::unknown; switch ((unsigned char)Magic[0]) { case 0x00: { @@ -250,7 +250,10 @@ if (Magic[1] == char(0xA6)) return file_magic::coff_object; break; - + case '#': + if (Magic[1] == '!') + return file_magic::aix_linker_import_file; + break; default: break; } diff --git a/llvm/lib/Object/AIXLinkerImportFile.cpp b/llvm/lib/Object/AIXLinkerImportFile.cpp new file mode 100644 --- /dev/null +++ b/llvm/lib/Object/AIXLinkerImportFile.cpp @@ -0,0 +1,27 @@ +//===- AIXLinkerImportFile.cpp -AIX linker Import file implement-*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This file implements the AIX Linker Import File interface. +// +//===----------------------------------------------------------------------===// + +#include "llvm/Object/AIXLinkerImportFile.h" +#include "llvm/BinaryFormat/Magic.h" + +namespace llvm { +namespace object { + +AIXLinkerImportFile::~AIXLinkerImportFile() = default; +Expected> +AIXLinkerImportFile::create(MemoryBufferRef Source) { + return std::make_unique( + file_magic::aix_linker_import_file, Source); +} + +} // namespace object +} // namespace llvm diff --git a/llvm/lib/Object/Binary.cpp b/llvm/lib/Object/Binary.cpp --- a/llvm/lib/Object/Binary.cpp +++ b/llvm/lib/Object/Binary.cpp @@ -13,6 +13,7 @@ #include "llvm/Object/Binary.h" #include "llvm/ADT/StringRef.h" #include "llvm/BinaryFormat/Magic.h" +#include "llvm/Object/AIXLinkerImportFile.h" #include "llvm/Object/Archive.h" #include "llvm/Object/Error.h" #include "llvm/Object/MachOUniversal.h" @@ -95,6 +96,8 @@ return MinidumpFile::create(Buffer); case file_magic::tapi_file: return TapiUniversal::create(Buffer); + case file_magic::aix_linker_import_file: + return AIXLinkerImportFile::create(Buffer); } llvm_unreachable("Unexpected Binary File Type"); } diff --git a/llvm/lib/Object/CMakeLists.txt b/llvm/lib/Object/CMakeLists.txt --- a/llvm/lib/Object/CMakeLists.txt +++ b/llvm/lib/Object/CMakeLists.txt @@ -1,4 +1,5 @@ add_llvm_component_library(LLVMObject + AIXLinkerImportFile.cpp Archive.cpp ArchiveWriter.cpp Binary.cpp diff --git a/llvm/lib/Object/ObjectFile.cpp b/llvm/lib/Object/ObjectFile.cpp --- a/llvm/lib/Object/ObjectFile.cpp +++ b/llvm/lib/Object/ObjectFile.cpp @@ -194,8 +194,9 @@ return createXCOFFObjectFile(Object, Binary::ID_XCOFF64); case file_magic::wasm_object: return createWasmObjectFile(Object); + default: + llvm_unreachable("Unexpected Object File Type"); } - llvm_unreachable("Unexpected Object File Type"); } Expected> diff --git a/llvm/test/tools/llvm-nm/XCOFF/export-symbols.test b/llvm/test/tools/llvm-nm/XCOFF/export-symbols.test --- a/llvm/test/tools/llvm-nm/XCOFF/export-symbols.test +++ b/llvm/test/tools/llvm-nm/XCOFF/export-symbols.test @@ -52,6 +52,10 @@ # RUN: yaml2obj -DFLAG=0x2000 --docnum=2 %s -o %t_shared.o # RUN: llvm-nm --export-symbols %t_shared.o | count 0 +## Test ignoring the parsing an imported symbol file that begins with #! +# RUN: echo "#!" > imp.txt +# RUN: llvm-nm --export-symbols imp.txt 2>&1 | FileCheck --allow-empty --implicit-check-not={{.}} %s + --- !XCOFF FileHeader: MagicNumber: 0x1DF