Mac: stop using NXArchInfo as a vocabulary type

It's deprecated in macOS 13/iOS 16, so this is an incremental step towards using newly introduced APIs for those OSes.

Since the description field is no longer available in the new
mach-o/util.h API, stop using it, especially since architecture name is
sufficiently informative.

Bug: chromium:1420654
Change-Id: If2cec4f1fc88d13a71f011822bff61f173486b68
Reviewed-on: https://chromium-review.googlesource.com/c/breakpad/breakpad/+/4322265
Reviewed-by: Mark Mentovai <mark@chromium.org>
This commit is contained in:
Leonard Grey 2023-03-09 09:54:53 -05:00
parent f5123d7196
commit ef55207540
6 changed files with 72 additions and 135 deletions

View file

@ -53,86 +53,44 @@
#define CPU_SUBTYPE_ARM64_E (static_cast<cpu_subtype_t>(2)) #define CPU_SUBTYPE_ARM64_E (static_cast<cpu_subtype_t>(2))
#endif // CPU_SUBTYPE_ARM64_E #endif // CPU_SUBTYPE_ARM64_E
namespace { std::optional<ArchInfo> GetArchInfoFromName(const char* arch_name) {
const NXArchInfo* ArchInfo_arm64(cpu_subtype_t cpu_subtype) {
const char* name = NULL;
switch (cpu_subtype) {
case CPU_SUBTYPE_ARM64_ALL:
name = "arm64";
break;
case CPU_SUBTYPE_ARM64_E:
name = "arm64e";
break;
default:
return NULL;
}
NXArchInfo* arm64 = new NXArchInfo;
*arm64 = *NXGetArchInfoFromCpuType(CPU_TYPE_ARM,
CPU_SUBTYPE_ARM_V7);
arm64->name = name;
arm64->cputype = CPU_TYPE_ARM64;
arm64->cpusubtype = cpu_subtype;
arm64->description = "arm 64";
return arm64;
}
const NXArchInfo* ArchInfo_armv7s() {
NXArchInfo* armv7s = new NXArchInfo;
*armv7s = *NXGetArchInfoFromCpuType(CPU_TYPE_ARM,
CPU_SUBTYPE_ARM_V7);
armv7s->name = "armv7s";
armv7s->cpusubtype = CPU_SUBTYPE_ARM_V7S;
armv7s->description = "arm v7s";
return armv7s;
}
} // namespace
namespace google_breakpad {
const NXArchInfo* BreakpadGetArchInfoFromName(const char* arch_name) {
// TODO: Remove this when the OS knows about arm64. // TODO: Remove this when the OS knows about arm64.
if (!strcmp("arm64", arch_name)) if (!strcmp("arm64", arch_name))
return BreakpadGetArchInfoFromCpuType(CPU_TYPE_ARM64, return ArchInfo{CPU_TYPE_ARM64, CPU_SUBTYPE_ARM64_ALL};
CPU_SUBTYPE_ARM64_ALL);
if (!strcmp("arm64e", arch_name)) if (!strcmp("arm64e", arch_name))
return BreakpadGetArchInfoFromCpuType(CPU_TYPE_ARM64, return ArchInfo{CPU_TYPE_ARM64, CPU_SUBTYPE_ARM64_E};
CPU_SUBTYPE_ARM64_E);
// TODO: Remove this when the OS knows about armv7s. // TODO: Remove this when the OS knows about armv7s.
if (!strcmp("armv7s", arch_name)) if (!strcmp("armv7s", arch_name))
return BreakpadGetArchInfoFromCpuType(CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V7S); return ArchInfo{CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V7S};
return NXGetArchInfoFromName(arch_name); const NXArchInfo* info = NXGetArchInfoFromName(arch_name);
if (info)
return ArchInfo{info->cputype, info->cpusubtype};
return std::nullopt;
} }
const NXArchInfo* BreakpadGetArchInfoFromCpuType(cpu_type_t cpu_type, const char* GetNameFromCPUType(cpu_type_t cpu_type, cpu_subtype_t cpu_subtype) {
cpu_subtype_t cpu_subtype) {
// TODO: Remove this when the OS knows about arm64. // TODO: Remove this when the OS knows about arm64.
if (cpu_type == CPU_TYPE_ARM64 && cpu_subtype == CPU_SUBTYPE_ARM64_ALL) { if (cpu_type == CPU_TYPE_ARM64 && cpu_subtype == CPU_SUBTYPE_ARM64_ALL) {
static const NXArchInfo* arm64 = ArchInfo_arm64(cpu_subtype); return "arm64";
return arm64;
} }
if (cpu_type == CPU_TYPE_ARM64 && cpu_subtype == CPU_SUBTYPE_ARM64_E) { if (cpu_type == CPU_TYPE_ARM64 && cpu_subtype == CPU_SUBTYPE_ARM64_E) {
static const NXArchInfo* arm64e = ArchInfo_arm64(cpu_subtype); return "arm64e";
return arm64e;
} }
// TODO: Remove this when the OS knows about armv7s. // TODO: Remove this when the OS knows about armv7s.
if (cpu_type == CPU_TYPE_ARM && cpu_subtype == CPU_SUBTYPE_ARM_V7S) { if (cpu_type == CPU_TYPE_ARM && cpu_subtype == CPU_SUBTYPE_ARM_V7S) {
static const NXArchInfo* armv7s = ArchInfo_armv7s(); return "armv7s";
return armv7s;
} }
return NXGetArchInfoFromCpuType(cpu_type, cpu_subtype); const NXArchInfo* info = NXGetArchInfoFromCpuType(cpu_type, cpu_subtype);
if (info)
return info->name;
return kUnknownArchName;
} }
} // namespace google_breakpad
// TODO(crbug.com/1242776): The "#ifndef __APPLE__" should be here, but the // TODO(crbug.com/1242776): The "#ifndef __APPLE__" should be here, but the
// system version of NXGetLocalArchInfo returns incorrect information on // system version of NXGetLocalArchInfo returns incorrect information on
// x86_64 machines (treating them as just x86), so use the Breakpad version // x86_64 machines (treating them as just x86), so use the Breakpad version
@ -207,7 +165,7 @@ const NXArchInfo kKnownArchitectures[] = {
} // namespace } // namespace
const NXArchInfo *NXGetLocalArchInfo(void) { ArchInfo GetLocalArchInfo(void) {
Architecture arch; Architecture arch;
#if defined(__i386__) #if defined(__i386__)
arch = kArch_i386; arch = kArch_i386;
@ -222,7 +180,8 @@ const NXArchInfo *NXGetLocalArchInfo(void) {
#else #else
#error "Unsupported CPU architecture" #error "Unsupported CPU architecture"
#endif #endif
return &kKnownArchitectures[arch]; NXArchInfo info = kKnownArchitectures[arch];
return {info.cputype, info.cpusubtype};
} }
#ifndef __APPLE__ #ifndef __APPLE__

View file

@ -31,16 +31,26 @@
#ifndef COMMON_MAC_ARCH_UTILITIES_H__ #ifndef COMMON_MAC_ARCH_UTILITIES_H__
#define COMMON_MAC_ARCH_UTILITIES_H__ #define COMMON_MAC_ARCH_UTILITIES_H__
#include <mach-o/arch.h> #include <mach/machine.h>
namespace google_breakpad { #include <optional>
// Custom implementation of |NXGetArchInfoFromName| and static constexpr const char* kUnknownArchName = "<Unknown architecture>";
// |NXGetArchInfoFromCpuType| that handle newer CPU on older OSes.
const NXArchInfo* BreakpadGetArchInfoFromName(const char* arch_name);
const NXArchInfo* BreakpadGetArchInfoFromCpuType(cpu_type_t cpu_type,
cpu_subtype_t cpu_subtype);
} // namespace google_breakpad struct ArchInfo {
cpu_type_t cputype;
cpu_subtype_t cpusubtype;
};
// Returns architecture info if `arch_name` corresponds to a valid, known
// architecture, and std::nullopt otherwise.
std::optional<ArchInfo> GetArchInfoFromName(const char* arch_name);
// Returns the name of the architecture specified by `cpu_type` and
// `cpu_subtype`, or `kUnknownArchName` if it's unknown or invalid.
const char* GetNameFromCPUType(cpu_type_t cpu_type, cpu_subtype_t cpu_subtype);
// Returns the architecture of the machine this code is running on.
ArchInfo GetLocalArchInfo();
#endif // COMMON_MAC_ARCH_UTILITIES_H__ #endif // COMMON_MAC_ARCH_UTILITIES_H__

View file

@ -221,11 +221,10 @@ bool DumpSymbols::ReadData(uint8_t* contents, size_t size,
return true; return true;
} }
bool DumpSymbols::SetArchitecture(cpu_type_t cpu_type, bool DumpSymbols::SetArchitecture(const ArchInfo& info) {
cpu_subtype_t cpu_subtype) {
// Find the best match for the architecture the user requested. // Find the best match for the architecture the user requested.
const SuperFatArch* best_match = FindBestMatchForArchitecture( const SuperFatArch* best_match =
cpu_type, cpu_subtype); FindBestMatchForArchitecture(info.cputype, info.cpusubtype);
if (!best_match) return false; if (!best_match) return false;
// Record the selected object file. // Record the selected object file.
@ -233,16 +232,6 @@ bool DumpSymbols::SetArchitecture(cpu_type_t cpu_type,
return true; return true;
} }
bool DumpSymbols::SetArchitecture(const std::string& arch_name) {
bool arch_set = false;
const NXArchInfo* arch_info =
google_breakpad::BreakpadGetArchInfoFromName(arch_name.c_str());
if (arch_info) {
arch_set = SetArchitecture(arch_info->cputype, arch_info->cpusubtype);
}
return arch_set;
}
SuperFatArch* DumpSymbols::FindBestMatchForArchitecture( SuperFatArch* DumpSymbols::FindBestMatchForArchitecture(
cpu_type_t cpu_type, cpu_subtype_t cpu_subtype) { cpu_type_t cpu_type, cpu_subtype_t cpu_subtype) {
// Check if all the object files can be converted to struct fat_arch. // Check if all the object files can be converted to struct fat_arch.
@ -402,8 +391,8 @@ bool DumpSymbols::CreateEmptyModule(scoped_ptr<Module>& module) {
selected_object_file_ = &object_files_[0]; selected_object_file_ = &object_files_[0];
else { else {
// Look for an object file whose architecture matches our own. // Look for an object file whose architecture matches our own.
const NXArchInfo* local_arch = NXGetLocalArchInfo(); ArchInfo local_arch = GetLocalArchInfo();
if (!SetArchitecture(local_arch->cputype, local_arch->cpusubtype)) { if (!SetArchitecture(local_arch)) {
fprintf(stderr, "%s: object file contains more than one" fprintf(stderr, "%s: object file contains more than one"
" architecture, none of which match the current" " architecture, none of which match the current"
" architecture; specify an architecture explicitly" " architecture; specify an architecture explicitly"
@ -418,18 +407,16 @@ bool DumpSymbols::CreateEmptyModule(scoped_ptr<Module>& module) {
// Find the name of the selected file's architecture, to appear in // Find the name of the selected file's architecture, to appear in
// the MODULE record and in error messages. // the MODULE record and in error messages.
const NXArchInfo* selected_arch_info = const char* selected_arch_name = GetNameFromCPUType(
google_breakpad::BreakpadGetArchInfoFromCpuType(
selected_object_file_->cputype, selected_object_file_->cpusubtype); selected_object_file_->cputype, selected_object_file_->cpusubtype);
// In certain cases, it is possible that architecture info can't be reliably // In certain cases, it is possible that architecture info can't be reliably
// determined, e.g. new architectures that breakpad is unware of. In that // determined, e.g. new architectures that breakpad is unware of. In that
// case, avoid crashing and return false instead. // case, avoid crashing and return false instead.
if (selected_arch_info == NULL) { if (selected_arch_name == kUnknownArchName) {
return false; return false;
} }
const char* selected_arch_name = selected_arch_info->name;
if (strcmp(selected_arch_name, "i386") == 0) if (strcmp(selected_arch_name, "i386") == 0)
selected_arch_name = "x86"; selected_arch_name = "x86";
@ -540,16 +527,14 @@ bool DumpSymbols::ReadCFI(google_breakpad::Module* module,
register_names = DwarfCFIToModule::RegisterNames::ARM64(); register_names = DwarfCFIToModule::RegisterNames::ARM64();
break; break;
default: { default: {
const NXArchInfo* arch = google_breakpad::BreakpadGetArchInfoFromCpuType( const char* arch_name = GetNameFromCPUType(macho_reader.cpu_type(),
macho_reader.cpu_type(), macho_reader.cpu_subtype()); macho_reader.cpu_subtype());
fprintf(stderr, "%s: cannot convert DWARF call frame information for ", fprintf(
selected_object_name_.c_str()); stderr,
if (arch) "%s: cannot convert DWARF call frame information for architecture "
fprintf(stderr, "architecture '%s'", arch->name); "'%s' (%d, %d) to Breakpad symbol file: no register name table\n",
else selected_object_name_.c_str(), arch_name, macho_reader.cpu_type(),
fprintf(stderr, "architecture %d,%d", macho_reader.cpu_subtype());
macho_reader.cpu_type(), macho_reader.cpu_subtype());
fprintf(stderr, " to Breakpad symbol file: no register name table\n");
return false; return false;
} }
} }

View file

@ -43,6 +43,7 @@
#include <vector> #include <vector>
#include "common/byte_cursor.h" #include "common/byte_cursor.h"
#include "common/mac/arch_utilities.h"
#include "common/mac/macho_reader.h" #include "common/mac/macho_reader.h"
#include "common/mac/super_fat_arch.h" #include "common/mac/super_fat_arch.h"
#include "common/module.h" #include "common/module.h"
@ -82,26 +83,15 @@ class DumpSymbols {
// problem reading |contents|, report it and return false. // problem reading |contents|, report it and return false.
bool ReadData(uint8_t* contents, size_t size, const std::string& filename); bool ReadData(uint8_t* contents, size_t size, const std::string& filename);
// If this dumper's file includes an object file for |cpu_type| and // If this dumper's file includes an object file for `info`, then select that
// |cpu_subtype|, then select that object file for dumping, and return // object file for dumping, and return true. Otherwise, return false, and
// true. Otherwise, return false, and leave this dumper's selected // leave this dumper's selected architecture unchanged.
// architecture unchanged.
// //
// By default, if this dumper's file contains only one object file, then // By default, if this dumper's file contains only one object file, then
// the dumper will dump those symbols; and if it contains more than one // the dumper will dump those symbols; and if it contains more than one
// object file, then the dumper will dump the object file whose // object file, then the dumper will dump the object file whose
// architecture matches that of this dumper program. // architecture matches that of this dumper program.
bool SetArchitecture(cpu_type_t cpu_type, cpu_subtype_t cpu_subtype); bool SetArchitecture(const ArchInfo& info);
// If this dumper's file includes an object file for |arch_name|, then select
// that object file for dumping, and return true. Otherwise, return false,
// and leave this dumper's selected architecture unchanged.
//
// By default, if this dumper's file contains only one object file, then
// the dumper will dump those symbols; and if it contains more than one
// object file, then the dumper will dump the object file whose
// architecture matches that of this dumper program.
bool SetArchitecture(const std::string& arch_name);
// Return a pointer to an array of SuperFatArch structures describing the // Return a pointer to an array of SuperFatArch structures describing the
// object files contained in this dumper's file. Set *|count| to the number // object files contained in this dumper's file. Set *|count| to the number

View file

@ -38,15 +38,15 @@
#include <assert.h> #include <assert.h>
#include <fcntl.h> #include <fcntl.h>
#include <mach-o/arch.h>
#include <mach-o/fat.h> #include <mach-o/fat.h>
#include <mach-o/loader.h> #include <mach-o/loader.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include "common/mac/arch_utilities.h"
#include "common/mac/byteswap.h" #include "common/mac/byteswap.h"
#include "common/mac/macho_walker.h"
#include "common/mac/macho_utilities.h" #include "common/mac/macho_utilities.h"
#include "common/mac/macho_walker.h"
namespace MacFileUtilities { namespace MacFileUtilities {
@ -85,9 +85,8 @@ bool MachoWalker::WalkHeader(cpu_type_t cpu_type, cpu_subtype_t cpu_subtype) {
cpu_subtype_t valid_cpu_subtype = cpu_subtype; cpu_subtype_t valid_cpu_subtype = cpu_subtype;
// if |cpu_type| is 0, use the native cpu type. // if |cpu_type| is 0, use the native cpu type.
if (cpu_type == 0) { if (cpu_type == 0) {
const NXArchInfo* arch = NXGetLocalArchInfo(); ArchInfo arch = GetLocalArchInfo();
assert(arch); valid_cpu_type = arch.cputype;
valid_cpu_type = arch->cputype;
valid_cpu_subtype = CPU_SUBTYPE_MULTIPLE; valid_cpu_subtype = CPU_SUBTYPE_MULTIPLE;
} }
off_t offset; off_t offset;

View file

@ -67,7 +67,7 @@ struct Options {
string srcPath; string srcPath;
string dsymPath; string dsymPath;
const NXArchInfo *arch; std::optional<ArchInfo> arch;
bool header_only; bool header_only;
bool cfi; bool cfi;
bool handle_inter_cu_refs; bool handle_inter_cu_refs;
@ -121,11 +121,12 @@ static void CopyCFIDataBetweenModules(Module* to_module,
} }
static bool SetArchitecture(DumpSymbols& dump_symbols, static bool SetArchitecture(DumpSymbols& dump_symbols,
const NXArchInfo* arch, const ArchInfo& arch,
const std::string& filename) { const std::string& filename) {
if (!dump_symbols.SetArchitecture(arch->cputype, arch->cpusubtype)) { if (!dump_symbols.SetArchitecture(arch)) {
fprintf(stderr, "%s: no architecture '%s' is present in file.\n", fprintf(stderr, "%s: no architecture '%s' is present in file.\n",
filename.c_str(), arch->name); filename.c_str(),
GetNameFromCPUType(arch.cputype, arch.cpusubtype));
size_t available_size; size_t available_size;
const SuperFatArch* available = const SuperFatArch* available =
dump_symbols.AvailableArchitectures(&available_size); dump_symbols.AvailableArchitectures(&available_size);
@ -135,14 +136,8 @@ static bool SetArchitecture(DumpSymbols& dump_symbols,
fprintf(stderr, "architectures present in the file are:\n"); fprintf(stderr, "architectures present in the file are:\n");
for (size_t i = 0; i < available_size; i++) { for (size_t i = 0; i < available_size; i++) {
const SuperFatArch* arch = &available[i]; const SuperFatArch* arch = &available[i];
const NXArchInfo* arch_info = fprintf(stderr, "%s\n",
google_breakpad::BreakpadGetArchInfoFromCpuType(arch->cputype, GetNameFromCPUType(arch->cputype, arch->cpusubtype));
arch->cpusubtype);
if (arch_info)
fprintf(stderr, "%s (%s)\n", arch_info->name, arch_info->description);
else
fprintf(stderr, "unrecognized cpu type 0x%x, subtype 0x%x\n",
arch->cputype, arch->cpusubtype);
} }
return false; return false;
} }
@ -173,7 +168,7 @@ static bool Start(const Options& options) {
return false; return false;
if (options.arch && if (options.arch &&
!SetArchitecture(dump_symbols, options.arch, primary_file)) { !SetArchitecture(dump_symbols, *options.arch, primary_file)) {
return false; return false;
} }
@ -193,7 +188,7 @@ static bool Start(const Options& options) {
return false; return false;
if (options.arch && if (options.arch &&
!SetArchitecture(dump_symbols, options.arch, options.srcPath)) { !SetArchitecture(dump_symbols, *options.arch, options.srcPath)) {
return false; return false;
} }
Module* cfi_module = NULL; Module* cfi_module = NULL;
@ -248,8 +243,7 @@ static void SetupOptions(int argc, const char *argv[], Options *options) {
options->header_only = true; options->header_only = true;
break; break;
case 'a': { case 'a': {
const NXArchInfo *arch_info = std::optional<ArchInfo> arch_info = GetArchInfoFromName(optarg);
google_breakpad::BreakpadGetArchInfoFromName(optarg);
if (!arch_info) { if (!arch_info) {
fprintf(stderr, "%s: Invalid architecture: %s\n", argv[0], optarg); fprintf(stderr, "%s: Invalid architecture: %s\n", argv[0], optarg);
Usage(argc, argv); Usage(argc, argv);