summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrad King <brad.king@kitware.com>2021-01-29 18:25:51 (GMT)
committerBrad King <brad.king@kitware.com>2021-02-03 17:26:58 (GMT)
commit56fc4a325f08465b725b08b3975cd51bdd2305c8 (patch)
treee92abaf4dd695b618db00ef568225a97a407f077
parentddaaee907dc617f6a763de37c368e50cd23a8a53 (diff)
downloadCMake-56fc4a325f08465b725b08b3975cd51bdd2305c8.zip
CMake-56fc4a325f08465b725b08b3975cd51bdd2305c8.tar.gz
CMake-56fc4a325f08465b725b08b3975cd51bdd2305c8.tar.bz2
cmXCOFF: Add helper to parse and edit the XCOFF binary format
-rw-r--r--Source/CMakeLists.txt10
-rw-r--r--Source/cmConfigure.cmake.h.in1
-rw-r--r--Source/cmXCOFF.cxx356
-rw-r--r--Source/cmXCOFF.h65
4 files changed, 432 insertions, 0 deletions
diff --git a/Source/CMakeLists.txt b/Source/CMakeLists.txt
index 26aaa79..6adc9cf 100644
--- a/Source/CMakeLists.txt
+++ b/Source/CMakeLists.txt
@@ -57,6 +57,10 @@ if(APPLE)
set(CMake_USE_MACH_PARSER 1)
endif()
+if(CMAKE_SYSTEM_NAME STREQUAL "AIX")
+ set(CMake_USE_XCOFF_PARSER 1)
+endif()
+
set(EXECUTABLE_OUTPUT_PATH ${CMake_BIN_DIR})
if(WIN32)
@@ -117,6 +121,11 @@ if(CMake_USE_MACH_PARSER)
set(MACH_SRCS cmMachO.h cmMachO.cxx)
endif()
+# Check if we can build the XCOFF parser.
+if(CMake_USE_XCOFF_PARSER)
+ set(XCOFF_SRCS cmXCOFF.h cmXCOFF.cxx)
+endif()
+
#
# Sources for CMakeLib
#
@@ -465,6 +474,7 @@ set(SRCS
cmWorkerPool.h
cmWorkingDirectory.cxx
cmWorkingDirectory.h
+ ${XCOFF_SRCS}
cmXMLParser.cxx
cmXMLParser.h
cmXMLSafe.cxx
diff --git a/Source/cmConfigure.cmake.h.in b/Source/cmConfigure.cmake.h.in
index 2fef246..aeca6b4 100644
--- a/Source/cmConfigure.cmake.h.in
+++ b/Source/cmConfigure.cmake.h.in
@@ -18,6 +18,7 @@
#cmakedefine HAVE_UNSETENV
#cmakedefine CMake_USE_ELF_PARSER
#cmakedefine CMake_USE_MACH_PARSER
+#cmakedefine CMake_USE_XCOFF_PARSER
#define CMake_DEFAULT_RECURSION_LIMIT @CMake_DEFAULT_RECURSION_LIMIT@
#define CMAKE_BIN_DIR "/@CMAKE_BIN_DIR@"
#define CMAKE_DATA_DIR "/@CMAKE_DATA_DIR@"
diff --git a/Source/cmXCOFF.cxx b/Source/cmXCOFF.cxx
new file mode 100644
index 0000000..890636e
--- /dev/null
+++ b/Source/cmXCOFF.cxx
@@ -0,0 +1,356 @@
+/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
+ file Copyright.txt or https://cmake.org/licensing for details. */
+#include "cmXCOFF.h"
+
+#include <algorithm>
+#include <cstddef>
+
+#include <cm/memory>
+
+#include "cmsys/FStream.hxx"
+
+#include "cmStringAlgorithms.h"
+
+// Include the XCOFF format information system header.
+#ifdef _AIX
+# define __XCOFF32__
+# define __XCOFF64__
+# include <xcoff.h>
+#else
+# error "This source may be compiled only on AIX."
+#endif
+
+class cmXCOFFInternal
+{
+public:
+ // Construct and take ownership of the file stream object.
+ cmXCOFFInternal(cmXCOFF* external, std::unique_ptr<std::iostream> fin,
+ cmXCOFF::Mode mode)
+ : External(external)
+ , Stream(std::move(fin))
+ , Mode(mode)
+ {
+ }
+
+ // Destruct and delete the file stream object.
+ virtual ~cmXCOFFInternal() = default;
+
+ cmXCOFF::Mode GetMode() const { return this->Mode; }
+
+ virtual cm::optional<cm::string_view> GetLibPath() = 0;
+
+ virtual bool SetLibPath(cm::string_view libPath) = 0;
+ virtual bool RemoveLibPath() = 0;
+
+protected:
+ // Data common to all ELF class implementations.
+
+ // The external cmXCOFF object.
+ cmXCOFF* External;
+
+ // The stream from which to read.
+ std::unique_ptr<std::iostream> Stream;
+
+ cmXCOFF::Mode Mode;
+
+ // Helper methods for subclasses.
+ void SetErrorMessage(const char* msg) { this->External->ErrorMessage = msg; }
+};
+
+namespace {
+
+struct XCOFF32
+{
+ typedef struct filehdr filehdr;
+ typedef struct aouthdr aouthdr;
+ typedef struct scnhdr scnhdr;
+ typedef struct ldhdr ldhdr;
+ static const std::size_t aouthdr_size = _AOUTHSZ_EXEC;
+};
+const unsigned char xcoff32_magic[] = { 0x01, 0xDF };
+
+struct XCOFF64
+{
+ typedef struct filehdr_64 filehdr;
+ typedef struct aouthdr_64 aouthdr;
+ typedef struct scnhdr_64 scnhdr;
+ typedef struct ldhdr_64 ldhdr;
+ static const std::size_t aouthdr_size = _AOUTHSZ_EXEC_64;
+};
+const unsigned char xcoff64_magic[] = { 0x01, 0xF7 };
+
+template <typename XCOFF>
+class Impl : public cmXCOFFInternal
+{
+ static_assert(sizeof(typename XCOFF::aouthdr) == XCOFF::aouthdr_size,
+ "aouthdr structure size matches _AOUTHSZ_EXEC macro");
+
+ typename XCOFF::filehdr FileHeader;
+ typename XCOFF::aouthdr AuxHeader;
+ typename XCOFF::scnhdr LoaderSectionHeader;
+ typename XCOFF::ldhdr LoaderHeader;
+
+ std::streamoff LoaderImportFileTablePos = 0;
+ std::vector<char> LoaderImportFileTable;
+
+ bool Read(typename XCOFF::filehdr& x)
+ {
+ // FIXME: Add byte swapping if needed.
+ return static_cast<bool>(
+ this->Stream->read(reinterpret_cast<char*>(&x), sizeof(x)));
+ }
+
+ bool Read(typename XCOFF::aouthdr& x)
+ {
+ // FIXME: Add byte swapping if needed.
+ return static_cast<bool>(
+ this->Stream->read(reinterpret_cast<char*>(&x), sizeof(x)));
+ }
+
+ bool Read(typename XCOFF::scnhdr& x)
+ {
+ // FIXME: Add byte swapping if needed.
+ return static_cast<bool>(
+ this->Stream->read(reinterpret_cast<char*>(&x), sizeof(x)));
+ }
+
+ bool Read(typename XCOFF::ldhdr& x)
+ {
+ // FIXME: Add byte swapping if needed.
+ return static_cast<bool>(
+ this->Stream->read(reinterpret_cast<char*>(&x), sizeof(x)));
+ }
+
+ bool WriteLoaderImportFileTableLength(decltype(XCOFF::ldhdr::l_istlen) x)
+ {
+ // FIXME: Add byte swapping if needed.
+ return static_cast<bool>(
+ this->Stream->write(reinterpret_cast<char const*>(&x), sizeof(x)));
+ }
+
+public:
+ Impl(cmXCOFF* external, std::unique_ptr<std::iostream> fin,
+ cmXCOFF::Mode mode);
+
+ cm::optional<cm::string_view> GetLibPath() override;
+ bool SetLibPath(cm::string_view libPath) override;
+ bool RemoveLibPath() override;
+};
+
+template <typename XCOFF>
+Impl<XCOFF>::Impl(cmXCOFF* external, std::unique_ptr<std::iostream> fin,
+ cmXCOFF::Mode mode)
+ : cmXCOFFInternal(external, std::move(fin), mode)
+{
+ if (!this->Read(this->FileHeader)) {
+ this->SetErrorMessage("Failed to read XCOFF file header.");
+ return;
+ }
+ if (this->FileHeader.f_opthdr != XCOFF::aouthdr_size) {
+ this->SetErrorMessage("XCOFF auxiliary header missing.");
+ return;
+ }
+ if (!this->Read(this->AuxHeader)) {
+ this->SetErrorMessage("Failed to read XCOFF auxiliary header.");
+ return;
+ }
+ if (this->AuxHeader.o_snloader == 0) {
+ this->SetErrorMessage("XCOFF loader section missing.");
+ return;
+ }
+ if (!this->Stream->seekg((this->AuxHeader.o_snloader - 1) *
+ sizeof(typename XCOFF::scnhdr),
+ std::ios::cur)) {
+ this->SetErrorMessage("Failed to seek to XCOFF loader section header.");
+ return;
+ }
+ if (!this->Read(this->LoaderSectionHeader)) {
+ this->SetErrorMessage("Failed to read XCOFF loader section header.");
+ return;
+ }
+ if ((this->LoaderSectionHeader.s_flags & STYP_LOADER) == 0) {
+ this->SetErrorMessage("XCOFF loader section header missing STYP_LOADER.");
+ return;
+ }
+ if (!this->Stream->seekg(this->LoaderSectionHeader.s_scnptr,
+ std::ios::beg)) {
+ this->SetErrorMessage("Failed to seek to XCOFF loader header.");
+ return;
+ }
+ if (!this->Read(this->LoaderHeader)) {
+ this->SetErrorMessage("Failed to read XCOFF loader header.");
+ return;
+ }
+ this->LoaderImportFileTablePos =
+ this->LoaderSectionHeader.s_scnptr + this->LoaderHeader.l_impoff;
+ if (!this->Stream->seekg(this->LoaderImportFileTablePos)) {
+ this->SetErrorMessage(
+ "Failed to seek to XCOFF loader import file id table.");
+ return;
+ }
+ this->LoaderImportFileTable.resize(this->LoaderHeader.l_istlen);
+ if (!this->Stream->read(this->LoaderImportFileTable.data(),
+ this->LoaderImportFileTable.size())) {
+ this->SetErrorMessage("Failed to read XCOFF loader import file id table.");
+ return;
+ }
+}
+
+template <typename XCOFF>
+cm::optional<cm::string_view> Impl<XCOFF>::GetLibPath()
+{
+ cm::optional<cm::string_view> result;
+ auto end = std::find(this->LoaderImportFileTable.begin(),
+ this->LoaderImportFileTable.end(), '\0');
+ if (end != this->LoaderImportFileTable.end()) {
+ result = cm::string_view(this->LoaderImportFileTable.data(),
+ end - this->LoaderImportFileTable.begin());
+ }
+ return result;
+}
+
+template <typename XCOFF>
+bool Impl<XCOFF>::SetLibPath(cm::string_view libPath)
+{
+ // The new LIBPATH must end in the standard AIX LIBPATH.
+#define CM_AIX_LIBPATH "/usr/lib:/lib"
+ std::string libPathBuf;
+ if (libPath != CM_AIX_LIBPATH &&
+ !cmHasLiteralSuffix(libPath, ":" CM_AIX_LIBPATH)) {
+ libPathBuf = std::string(libPath);
+ if (!libPathBuf.empty() && libPathBuf.back() != ':') {
+ libPathBuf.push_back(':');
+ }
+ libPathBuf += CM_AIX_LIBPATH;
+ libPath = libPathBuf;
+ }
+#undef CM_AIX_LIBPATH
+
+ auto oldEnd = std::find(this->LoaderImportFileTable.begin(),
+ this->LoaderImportFileTable.end(), '\0');
+ if (oldEnd == this->LoaderImportFileTable.end()) {
+ this->SetErrorMessage("XCOFF loader import file id table is invalid.");
+ return false;
+ }
+ if ((this->LoaderImportFileTable.begin() + libPath.size()) > oldEnd) {
+ this->SetErrorMessage("XCOFF loader import file id table is too small.");
+ return false;
+ }
+
+ {
+ std::vector<char> ift;
+ ift.reserve(this->LoaderImportFileTable.size());
+ // Start with the new LIBPATH.
+ ift.insert(ift.end(), libPath.begin(), libPath.end());
+ // Add the rest of the original table.
+ ift.insert(ift.end(), oldEnd, this->LoaderImportFileTable.end());
+ // If the new table is shorter, zero out the leftover space.
+ ift.resize(this->LoaderImportFileTable.size(), 0);
+ this->LoaderHeader.l_istlen =
+ static_cast<decltype(XCOFF::ldhdr::l_istlen)>(ift.size());
+ this->LoaderImportFileTable = std::move(ift);
+ }
+
+ if (!this->Stream->seekp(this->LoaderSectionHeader.s_scnptr +
+ offsetof(typename XCOFF::ldhdr, l_istlen),
+ std::ios::beg)) {
+ this->SetErrorMessage(
+ "Failed to seek to XCOFF loader header import file id table length.");
+ return false;
+ }
+ if (!this->WriteLoaderImportFileTableLength(this->LoaderHeader.l_istlen)) {
+ this->SetErrorMessage(
+ "Failed to write XCOFF loader header import file id table length.");
+ return false;
+ }
+ if (!this->Stream->seekp(this->LoaderImportFileTablePos, std::ios::beg)) {
+ this->SetErrorMessage(
+ "Failed to seek to XCOFF loader import file id table.");
+ return false;
+ }
+ if (!this->Stream->write(this->LoaderImportFileTable.data(),
+ this->LoaderImportFileTable.size())) {
+ this->SetErrorMessage(
+ "Failed to write XCOFF loader import file id table.");
+ return false;
+ }
+
+ return true;
+}
+
+template <typename XCOFF>
+bool Impl<XCOFF>::RemoveLibPath()
+{
+ return this->SetLibPath({});
+}
+}
+
+//============================================================================
+// External class implementation.
+
+cmXCOFF::cmXCOFF(const char* fname, Mode mode)
+{
+ // Try to open the file.
+ std::ios::openmode fmode = std::ios::in | std::ios::binary;
+ if (mode == Mode::ReadWrite) {
+ fmode |= std::ios::out;
+ }
+ auto f = cm::make_unique<cmsys::fstream>(fname, fmode);
+
+ // Quit now if the file could not be opened.
+ if (!f || !*f) {
+ this->ErrorMessage = "Error opening input file.";
+ return;
+ }
+
+ // Read the XCOFF magic number.
+ unsigned char magic[2];
+ if (!f->read(reinterpret_cast<char*>(magic), sizeof(magic))) {
+ this->ErrorMessage = "Error reading XCOFF magic number.";
+ return;
+ }
+ if (!f->seekg(0)) {
+ this->ErrorMessage = "Error seeking to beginning of file.";
+ return;
+ }
+
+ // Check the XCOFF type.
+ if (magic[0] == xcoff32_magic[0] && magic[1] == xcoff32_magic[1]) {
+ this->Internal = cm::make_unique<Impl<XCOFF32>>(this, std::move(f), mode);
+ } else if (magic[0] == xcoff64_magic[0] && magic[1] == xcoff64_magic[1]) {
+ this->Internal = cm::make_unique<Impl<XCOFF64>>(this, std::move(f), mode);
+ } else {
+ this->ErrorMessage = "File is not a XCOFF32 or XCOFF64 binary.";
+ }
+}
+
+cmXCOFF::~cmXCOFF() = default;
+
+cmXCOFF::cmXCOFF(cmXCOFF&&) = default;
+cmXCOFF& cmXCOFF::operator=(cmXCOFF&&) = default;
+
+bool cmXCOFF::Valid() const
+{
+ return this->Internal && this->ErrorMessage.empty();
+}
+
+cm::optional<cm::string_view> cmXCOFF::GetLibPath() const
+{
+ cm::optional<cm::string_view> result;
+ if (this->Valid()) {
+ result = this->Internal->GetLibPath();
+ }
+ return result;
+}
+
+bool cmXCOFF::SetLibPath(cm::string_view libPath)
+{
+ return this->Valid() && this->Internal->GetMode() == Mode::ReadWrite &&
+ this->Internal->SetLibPath(libPath);
+}
+
+bool cmXCOFF::RemoveLibPath()
+{
+ return this->Valid() && this->Internal->GetMode() == Mode::ReadWrite &&
+ this->Internal->RemoveLibPath();
+}
diff --git a/Source/cmXCOFF.h b/Source/cmXCOFF.h
new file mode 100644
index 0000000..16cda9d
--- /dev/null
+++ b/Source/cmXCOFF.h
@@ -0,0 +1,65 @@
+/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
+ file Copyright.txt or https://cmake.org/licensing for details. */
+#pragma once
+
+#include "cmConfigure.h" // IWYU pragma: keep
+
+#include <iosfwd>
+#include <memory>
+#include <string>
+
+#include <cm/optional>
+#include <cm/string_view>
+
+#if !defined(CMake_USE_XCOFF_PARSER)
+# error "This file may be included only if CMake_USE_XCOFF_PARSER is enabled."
+#endif
+
+class cmXCOFFInternal;
+
+/** \class cmXCOFF
+ * \brief XCOFF parser.
+ */
+class cmXCOFF
+{
+public:
+ enum class Mode
+ {
+ ReadOnly,
+ ReadWrite
+ };
+
+ /** Construct with the name of the XCOFF input file to parse. */
+ cmXCOFF(const char* fname, Mode = Mode::ReadOnly);
+
+ /** Destruct. */
+ ~cmXCOFF();
+
+ cmXCOFF(cmXCOFF&&);
+ cmXCOFF(cmXCOFF const&) = delete;
+ cmXCOFF& operator=(cmXCOFF&&);
+ cmXCOFF& operator=(cmXCOFF const&) = delete;
+
+ /** Get the error message if any. */
+ std::string const& GetErrorMessage() const { return this->ErrorMessage; }
+
+ /** Boolean conversion. True if the XCOFF file is valid. */
+ explicit operator bool() const { return this->Valid(); }
+
+ /** Get the LIBPATH (RPATH) parsed from the file, if any. */
+ cm::optional<cm::string_view> GetLibPath() const;
+
+ /** Set the LIBPATH (RPATH).
+ Works only if cmXCOFF was constructed with Mode::ReadWrite. */
+ bool SetLibPath(cm::string_view libPath);
+
+ /** Remove the LIBPATH (RPATH).
+ Works only if cmXCOFF was constructed with Mode::ReadWrite. */
+ bool RemoveLibPath();
+
+private:
+ friend class cmXCOFFInternal;
+ bool Valid() const;
+ std::unique_ptr<cmXCOFFInternal> Internal;
+ std::string ErrorMessage;
+};