Skip to content

Commit

Permalink
Merge topic 'file-grd-arch' into release-3.21
Browse files Browse the repository at this point in the history
b2c0334 file(GET_RUNTIME_DEPENDENCIES): Check architecture of dependencies

Acked-by: Kitware Robot <kwrobot@kitware.com>
Merge-request: !6339
  • Loading branch information
bradking authored and kwrobot committed Jul 13, 2021
2 parents 49e1fda + b2c0334 commit 00e8292
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 6 deletions.
43 changes: 41 additions & 2 deletions Source/cmBinUtilsLinuxELFLinker.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
#include "cmStringAlgorithms.h"
#include "cmSystemTools.h"

#ifdef CMake_USE_ELF_PARSER
# include "cmELF.h"
#endif

static std::string ReplaceOrigin(const std::string& rpath,
const std::string& origin)
{
Expand Down Expand Up @@ -86,6 +90,24 @@ bool cmBinUtilsLinuxELFLinker::ScanDependencies(
std::string const& file, cmStateEnums::TargetType /* unused */)
{
std::vector<std::string> parentRpaths;

#ifdef CMake_USE_ELF_PARSER
cmELF elf(file.c_str());
if (!elf) {
return false;
}
if (elf.GetMachine() != 0) {
if (this->Machine != 0) {
if (elf.GetMachine() != this->Machine) {
this->SetError("All files must have the same architecture.");
return false;
}
} else {
this->Machine = elf.GetMachine();
}
}
#endif

return this->ScanDependencies(file, parentRpaths);
}

Expand Down Expand Up @@ -150,21 +172,38 @@ bool cmBinUtilsLinuxELFLinker::ScanDependencies(
return true;
}

namespace {
bool FileHasArchitecture(const char* filename, std::uint16_t machine)
{
#ifdef CMake_USE_ELF_PARSER
cmELF elf(filename);
if (!elf) {
return false;
}
return machine == 0 || machine == elf.GetMachine();
#else
return true;
#endif
}
}

bool cmBinUtilsLinuxELFLinker::ResolveDependency(
std::string const& name, std::vector<std::string> const& searchPaths,
std::string& path, bool& resolved)
{
for (auto const& searchPath : searchPaths) {
path = cmStrCat(searchPath, '/', name);
if (cmSystemTools::PathExists(path)) {
if (cmSystemTools::PathExists(path) &&
FileHasArchitecture(path.c_str(), this->Machine)) {
resolved = true;
return true;
}
}

for (auto const& searchPath : this->Archive->GetSearchDirectories()) {
path = cmStrCat(searchPath, '/', name);
if (cmSystemTools::PathExists(path)) {
if (cmSystemTools::PathExists(path) &&
FileHasArchitecture(path.c_str(), this->Machine)) {
std::ostringstream warning;
warning << "Dependency " << name << " found in search directory:\n "
<< searchPath
Expand Down
4 changes: 4 additions & 0 deletions Source/cmBinUtilsLinuxELFLinker.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

#pragma once

#include "cmConfigure.h" // IWYU pragma: keep

#include <cstdint>
#include <memory>
#include <string>
#include <vector>
Expand All @@ -29,6 +32,7 @@ class cmBinUtilsLinuxELFLinker : public cmBinUtilsLinker
std::unique_ptr<cmLDConfigTool> LDConfigTool;
bool HaveLDConfigPaths = false;
std::vector<std::string> LDConfigPaths;
std::uint16_t Machine = 0;

bool ScanDependencies(std::string const& file,
std::vector<std::string> const& parentRpaths);
Expand Down
16 changes: 16 additions & 0 deletions Source/cmELF.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ class cmELFInternal
// Return the recorded ELF type.
cmELF::FileType GetFileType() const { return this->ELFType; }

// Return the recorded machine.
std::uint16_t GetMachine() const { return this->Machine; }

protected:
// Data common to all ELF class implementations.

Expand All @@ -183,6 +186,9 @@ class cmELFInternal
// The ELF file type.
cmELF::FileType ELFType;

// The ELF architecture.
std::uint16_t Machine;

// Whether we need to byte-swap structures read from the stream.
bool NeedSwap;

Expand Down Expand Up @@ -478,6 +484,8 @@ cmELFInternalImpl<Types>::cmELFInternalImpl(cmELF* external,
}
}

this->Machine = this->ELFHeader.e_machine;

// Load the section headers.
this->SectionHeaders.resize(this->ELFHeader.e_shnum);
for (ELF_Half i = 0; i < this->ELFHeader.e_shnum; ++i) {
Expand Down Expand Up @@ -757,6 +765,14 @@ cmELF::FileType cmELF::GetFileType() const
return FileTypeInvalid;
}

std::uint16_t cmELF::GetMachine() const
{
if (this->Valid()) {
return this->Internal->GetMachine();
}
return 0;
}

unsigned int cmELF::GetNumberOfSections() const
{
if (this->Valid()) {
Expand Down
4 changes: 4 additions & 0 deletions Source/cmELF.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "cmConfigure.h" // IWYU pragma: keep

#include <cstdint>
#include <iosfwd>
#include <memory>
#include <string>
Expand Down Expand Up @@ -72,6 +73,9 @@ class cmELF
/** Get the type of the file opened. */
FileType GetFileType() const;

/** Get the machine of the file opened. */
std::uint16_t GetMachine() const;

/** Get the number of ELF sections present. */
unsigned int GetNumberOfSections() const;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
^CMake Error at cmake_install\.cmake:[0-9]+ \(file\):
file Failed to run objdump on:

[^
]*/Tests/RunCMake/file-GET_RUNTIME_DEPENDENCIES/linux-notfile-build/root-all/bin/\.\./lib/libtest\.so$
file Could not resolve file libtest\.so$

0 comments on commit 00e8292

Please sign in to comment.