Skip to content

Commit 356799b

Browse files
committed
Address reviewer feedback.
- Add getLoadCommandName() helper to map cmd values to names - Change to indexed loop to show load command index for disambiguation - Update warning format to match llvm-readobj style: Known commands: "load command 0 LC_SEGMENT_64 cmdsize too small" Unknown commands: "load command 0 (0xdeadbeef) cmdsize too small" - Use WithColor::warning()
1 parent 1857805 commit 356799b

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

llvm/lib/ObjectYAML/MachOEmitter.cpp

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,26 @@
1919
#include "llvm/Support/Error.h"
2020
#include "llvm/Support/FormatVariadic.h"
2121
#include "llvm/Support/LEB128.h"
22-
#include "llvm/Support/YAMLTraits.h"
2322
#include "llvm/Support/SystemZ/zOSSupport.h"
23+
#include "llvm/Support/WithColor.h"
24+
#include "llvm/Support/YAMLTraits.h"
2425
#include "llvm/Support/raw_ostream.h"
2526

2627
using namespace llvm;
2728

2829
namespace {
2930

31+
static const char *getLoadCommandName(uint32_t cmd) {
32+
switch (cmd) {
33+
#define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) \
34+
case MachO::LCName: \
35+
return #LCName;
36+
#include "llvm/BinaryFormat/MachO.def"
37+
default:
38+
return nullptr;
39+
}
40+
}
41+
3042
class MachOWriter {
3143
public:
3244
MachOWriter(MachOYAML::Object &Obj) : Obj(Obj), fileStart(0) {
@@ -244,7 +256,8 @@ void MachOWriter::ZeroToOffset(raw_ostream &OS, size_t Offset) {
244256
}
245257

246258
void MachOWriter::writeLoadCommands(raw_ostream &OS) {
247-
for (auto &LC : Obj.LoadCommands) {
259+
for (size_t i = 0; i < Obj.LoadCommands.size(); ++i) {
260+
auto &LC = Obj.LoadCommands[i];
248261
size_t BytesWritten = 0;
249262
llvm::MachO::macho_load_command Data = LC.Data;
250263

@@ -287,9 +300,18 @@ void MachOWriter::writeLoadCommands(raw_ostream &OS) {
287300
// specified test cases.
288301
// Prevent integer underflow if BytesWritten exceeds cmdsize.
289302
if (BytesWritten > LC.Data.load_command_data.cmdsize) {
290-
errs() << "warning: load command " << LC.Data.load_command_data.cmd
291-
<< " cmdsize too small (" << LC.Data.load_command_data.cmdsize
292-
<< " bytes) for actual size (" << BytesWritten << " bytes)\n";
303+
const char *name = getLoadCommandName(LC.Data.load_command_data.cmd);
304+
if (name)
305+
WithColor::warning()
306+
<< "load command " << i << " " << name << " cmdsize too small ("
307+
<< LC.Data.load_command_data.cmdsize << " bytes) for actual size ("
308+
<< BytesWritten << " bytes)\n";
309+
else
310+
WithColor::warning()
311+
<< "load command " << i << " (0x"
312+
<< Twine::utohexstr(LC.Data.load_command_data.cmd)
313+
<< ") cmdsize too small (" << LC.Data.load_command_data.cmdsize
314+
<< " bytes) for actual size (" << BytesWritten << " bytes)\n";
293315
}
294316
auto BytesRemaining = (BytesWritten < LC.Data.load_command_data.cmdsize)
295317
? LC.Data.load_command_data.cmdsize - BytesWritten

llvm/test/ObjectYAML/MachO/segment-cmdsize-too-small.yaml renamed to llvm/test/ObjectYAML/MachO/load-cmdsize-too-small.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
## Test that yaml2obj handles cmdsize that is too small for LC_SEGMENT_64
2-
## without crashing (due to integer underflow).
1+
## Test that yaml2obj handles load commands with cmdsize smaller than the
2+
## actual structure size without crashing (due to integer underflow).
33

44
# RUN: yaml2obj %s -o %t 2>&1 | FileCheck %s --check-prefix=WARNING
55
# RUN: not llvm-readobj --file-headers %t 2>&1 | FileCheck %s --check-prefix=MALFORMED
66

7-
# WARNING: warning: load command 25 cmdsize too small (56 bytes) for actual size (72 bytes)
7+
# WARNING: warning: load command 0 LC_SEGMENT_64 cmdsize too small (56 bytes) for actual size (72 bytes)
88

99
# MALFORMED: error: {{.*}}: truncated or malformed object (load command 0 LC_SEGMENT_64 cmdsize too small)
1010

0 commit comments

Comments
 (0)