Skip to content

Commit 5695096

Browse files
committed
Print a warning for note segments and note sections in segments
1 parent a4c8edb commit 5695096

File tree

12 files changed

+165
-64
lines changed

12 files changed

+165
-64
lines changed

llvm/include/llvm/ObjCopy/ELF/ELFObjcopy.h

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@
99
#ifndef LLVM_OBJCOPY_ELF_ELFOBJCOPY_H
1010
#define LLVM_OBJCOPY_ELF_ELFOBJCOPY_H
1111

12+
#include "llvm/ADT/STLFunctionalExtras.h"
13+
1214
namespace llvm {
1315
class Error;
1416
class MemoryBuffer;
1517
class raw_ostream;
18+
class Twine;
1619

1720
namespace object {
1821
class ELFObjectFileBase;
@@ -25,26 +28,29 @@ struct ELFConfig;
2528
namespace elf {
2629
/// Apply the transformations described by \p Config and \p ELFConfig to
2730
/// \p In, which must represent an IHex file, and writes the result
28-
/// into \p Out.
31+
/// into \p Out. Warnings can be printed via \p WarningCallback.
2932
/// \returns any Error encountered whilst performing the operation.
3033
Error executeObjcopyOnIHex(const CommonConfig &Config,
3134
const ELFConfig &ELFConfig, MemoryBuffer &In,
32-
raw_ostream &Out);
35+
raw_ostream &Out,
36+
function_ref<void(const Twine &)> WarningCallback);
3337

3438
/// Apply the transformations described by \p Config and \p ELFConfig to
3539
/// \p In, which is treated as a raw binary input, and writes the result
36-
/// into \p Out.
40+
/// into \p Out. Warnings can be printed via \p WarningCallback.
3741
/// \returns any Error encountered whilst performing the operation.
38-
Error executeObjcopyOnRawBinary(const CommonConfig &Config,
39-
const ELFConfig &ELFConfig, MemoryBuffer &In,
40-
raw_ostream &Out);
42+
Error executeObjcopyOnRawBinary(
43+
const CommonConfig &Config, const ELFConfig &ELFConfig, MemoryBuffer &In,
44+
raw_ostream &Out, function_ref<void(const Twine &)> WarningCallback);
4145

4246
/// Apply the transformations described by \p Config and \p ELFConfig to
43-
/// \p In and writes the result into \p Out.
47+
/// \p In and writes the result into \p Out. Warnings can be printed via
48+
/// \p WarningCallback.
4449
/// \returns any Error encountered whilst performing the operation.
4550
Error executeObjcopyOnBinary(const CommonConfig &Config,
4651
const ELFConfig &ELFConfig,
47-
object::ELFObjectFileBase &In, raw_ostream &Out);
52+
object::ELFObjectFileBase &In, raw_ostream &Out,
53+
function_ref<void(const Twine &)> WarningCallback);
4854

4955
} // end namespace elf
5056
} // end namespace objcopy

llvm/include/llvm/ObjCopy/MachO/MachOObjcopy.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@
99
#ifndef LLVM_OBJCOPY_MACHO_MACHOOBJCOPY_H
1010
#define LLVM_OBJCOPY_MACHO_MACHOOBJCOPY_H
1111

12+
#include "llvm/ADT/STLFunctionalExtras.h"
13+
1214
namespace llvm {
1315
class Error;
1416
class raw_ostream;
17+
class Twine;
1518

1619
namespace object {
1720
class MachOObjectFile;
@@ -26,17 +29,20 @@ class MultiFormatConfig;
2629
namespace macho {
2730
/// Apply the transformations described by \p Config and \p MachOConfig to
2831
/// \p In and writes the result into \p Out.
32+
/// Warnings can be printed via \p WarningCallback.
2933
/// \returns any Error encountered whilst performing the operation.
3034
Error executeObjcopyOnBinary(const CommonConfig &Config,
3135
const MachOConfig &MachOConfig,
32-
object::MachOObjectFile &In, raw_ostream &Out);
36+
object::MachOObjectFile &In, raw_ostream &Out,
37+
function_ref<void(const Twine &)> WarningCallback);
3338

3439
/// Apply the transformations described by \p Config and \p MachOConfig to
3540
/// \p In and writes the result into \p Out.
41+
/// Warnings can be printed via \p WarningCallback.
3642
/// \returns any Error encountered whilst performing the operation.
3743
Error executeObjcopyOnMachOUniversalBinary(
3844
const MultiFormatConfig &Config, const object::MachOUniversalBinary &In,
39-
raw_ostream &Out);
45+
raw_ostream &Out, function_ref<void(const Twine &)> WarningCallback);
4046

4147
} // end namespace macho
4248
} // end namespace objcopy

llvm/include/llvm/ObjCopy/ObjCopy.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
#ifndef LLVM_OBJCOPY_OBJCOPY_H
1010
#define LLVM_OBJCOPY_OBJCOPY_H
1111

12+
#include "llvm/ADT/STLFunctionalExtras.h"
1213
#include "llvm/Support/Error.h"
1314

1415
namespace llvm {
1516
class raw_ostream;
17+
class Twine;
1618

1719
namespace object {
1820
class Archive;
@@ -25,16 +27,20 @@ class MultiFormatConfig;
2527
/// Applies the transformations described by \p Config to
2628
/// each member in archive \p Ar.
2729
/// Writes a result in a file specified by \p Config.OutputFilename.
30+
/// Warnings can be printed via \p WarningCallback.
2831
/// \returns any Error encountered whilst performing the operation.
29-
Error executeObjcopyOnArchive(const MultiFormatConfig &Config,
30-
const object::Archive &Ar);
32+
Error executeObjcopyOnArchive(
33+
const MultiFormatConfig &Config, const object::Archive &Ar,
34+
function_ref<void(const Twine &)> WarningCallback);
3135

3236
/// Applies the transformations described by \p Config to \p In and writes
3337
/// the result into \p Out. This function does the dispatch based on the
3438
/// format of the input binary (COFF, ELF, MachO or wasm).
39+
/// Warnings can be printed via \p WarningCallback.
3540
/// \returns any Error encountered whilst performing the operation.
3641
Error executeObjcopyOnBinary(const MultiFormatConfig &Config,
37-
object::Binary &In, raw_ostream &Out);
42+
object::Binary &In, raw_ostream &Out,
43+
function_ref<void(const Twine &)> WarningCallback);
3844

3945
} // end namespace objcopy
4046
} // end namespace llvm

llvm/lib/ObjCopy/Archive.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ namespace objcopy {
2020
using namespace llvm::object;
2121

2222
Expected<std::vector<NewArchiveMember>>
23-
createNewArchiveMembers(const MultiFormatConfig &Config, const Archive &Ar) {
23+
createNewArchiveMembers(const MultiFormatConfig &Config, const Archive &Ar,
24+
function_ref<void(const Twine &)> WarningCallback) {
2425
std::vector<NewArchiveMember> NewArchiveMembers;
2526
Error Err = Error::success();
2627
for (const Archive::Child &Child : Ar.children(Err)) {
@@ -36,7 +37,8 @@ createNewArchiveMembers(const MultiFormatConfig &Config, const Archive &Ar) {
3637
SmallVector<char, 0> Buffer;
3738
raw_svector_ostream MemStream(Buffer);
3839

39-
if (Error E = executeObjcopyOnBinary(Config, *ChildOrErr->get(), MemStream))
40+
if (Error E = executeObjcopyOnBinary(Config, *ChildOrErr->get(), MemStream,
41+
WarningCallback))
4042
return std::move(E);
4143

4244
Expected<NewArchiveMember> Member = NewArchiveMember::getOldMember(
@@ -94,10 +96,11 @@ static Error deepWriteArchive(StringRef ArcName,
9496
return Error::success();
9597
}
9698

97-
Error executeObjcopyOnArchive(const MultiFormatConfig &Config,
98-
const object::Archive &Ar) {
99+
Error executeObjcopyOnArchive(
100+
const MultiFormatConfig &Config, const object::Archive &Ar,
101+
function_ref<void(const Twine &)> WarningCallback) {
99102
Expected<std::vector<NewArchiveMember>> NewArchiveMembersOrErr =
100-
createNewArchiveMembers(Config, Ar);
103+
createNewArchiveMembers(Config, Ar, WarningCallback);
101104
if (!NewArchiveMembersOrErr)
102105
return NewArchiveMembersOrErr.takeError();
103106
const CommonConfig &CommonConfig = Config.getCommonConfig();

llvm/lib/ObjCopy/Archive.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ class MultiFormatConfig;
2020

2121
/// Applies the transformations described by \p Config to
2222
/// each member in archive \p Ar.
23+
/// Warnings can be printed via \p WarningCallback.
2324
/// \returns Vector of transformed archive members.
2425
Expected<std::vector<NewArchiveMember>>
2526
createNewArchiveMembers(const MultiFormatConfig &Config,
26-
const object::Archive &Ar);
27+
const object::Archive &Ar,
28+
function_ref<void(const Twine &)> WarningCallback);
2729

2830
} // end namespace objcopy
2931
} // end namespace llvm

llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -671,11 +671,24 @@ RemoveNoteDetail::updateData(ArrayRef<uint8_t> OldData,
671671
}
672672

673673
static Error removeNote(Object &Obj, endianness Endianness,
674-
ArrayRef<RemoveNoteInfo> NotesToRemove) {
674+
ArrayRef<RemoveNoteInfo> NotesToRemove,
675+
function_ref<void(const Twine &)> WarningCallback) {
676+
// TODO: Support note segments.
677+
for (Segment &Seg : Obj.segments()) {
678+
if (Seg.Type == PT_NOTE) {
679+
WarningCallback("note segments are not supported");
680+
break;
681+
}
682+
}
675683
for (auto &Sec : Obj.sections()) {
676-
// TODO: Support note sections in segments
677-
if (Sec.Type != SHT_NOTE || Sec.ParentSegment || !Sec.hasContents())
684+
if (Sec.Type != SHT_NOTE || !Sec.hasContents())
678685
continue;
686+
// TODO: Support note sections in segments.
687+
if (Sec.ParentSegment) {
688+
WarningCallback("cannot remove note(s) from " + Sec.Name +
689+
": sections in segments are not supported");
690+
continue;
691+
}
679692
ArrayRef<uint8_t> OldData = Sec.getContents();
680693
size_t Align = std::max<size_t>(4, Sec.Align);
681694
// Note: notes for both 32-bit and 64-bit ELF files use 4-byte words in the
@@ -759,7 +772,8 @@ static Error verifyNoteSection(StringRef Name, endianness Endianness,
759772
// depend a) on the order the options occur in or b) on some opaque priority
760773
// system. The only priority is that keeps/copies overrule removes.
761774
static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig,
762-
ElfType OutputElfType, Object &Obj) {
775+
ElfType OutputElfType, Object &Obj,
776+
function_ref<void(const Twine &)> WarningCallback) {
763777
if (Config.OutputArch) {
764778
Obj.Machine = Config.OutputArch->EMachine;
765779
Obj.OSABI = Config.OutputArch->OSABI;
@@ -885,7 +899,8 @@ static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig,
885899
: endianness::big;
886900

887901
if (!ELFConfig.NotesToRemove.empty()) {
888-
if (Error Err = removeNote(Obj, E, ELFConfig.NotesToRemove))
902+
if (Error Err =
903+
removeNote(Obj, E, ELFConfig.NotesToRemove, WarningCallback))
889904
return Err;
890905
}
891906

@@ -1020,25 +1035,25 @@ static Error writeOutput(const CommonConfig &Config, Object &Obj,
10201035
return Writer->write();
10211036
}
10221037

1023-
Error objcopy::elf::executeObjcopyOnIHex(const CommonConfig &Config,
1024-
const ELFConfig &ELFConfig,
1025-
MemoryBuffer &In, raw_ostream &Out) {
1038+
Error objcopy::elf::executeObjcopyOnIHex(
1039+
const CommonConfig &Config, const ELFConfig &ELFConfig, MemoryBuffer &In,
1040+
raw_ostream &Out, function_ref<void(const Twine &)> WarningCallback) {
10261041
IHexReader Reader(&In);
10271042
Expected<std::unique_ptr<Object>> Obj = Reader.create(true);
10281043
if (!Obj)
10291044
return Obj.takeError();
10301045

10311046
const ElfType OutputElfType =
10321047
getOutputElfType(Config.OutputArch.value_or(MachineInfo()));
1033-
if (Error E = handleArgs(Config, ELFConfig, OutputElfType, **Obj))
1048+
if (Error E =
1049+
handleArgs(Config, ELFConfig, OutputElfType, **Obj, WarningCallback))
10341050
return E;
10351051
return writeOutput(Config, **Obj, Out, OutputElfType);
10361052
}
10371053

1038-
Error objcopy::elf::executeObjcopyOnRawBinary(const CommonConfig &Config,
1039-
const ELFConfig &ELFConfig,
1040-
MemoryBuffer &In,
1041-
raw_ostream &Out) {
1054+
Error objcopy::elf::executeObjcopyOnRawBinary(
1055+
const CommonConfig &Config, const ELFConfig &ELFConfig, MemoryBuffer &In,
1056+
raw_ostream &Out, function_ref<void(const Twine &)> WarningCallback) {
10421057
BinaryReader Reader(&In, ELFConfig.NewSymbolVisibility);
10431058
Expected<std::unique_ptr<Object>> Obj = Reader.create(true);
10441059
if (!Obj)
@@ -1048,15 +1063,16 @@ Error objcopy::elf::executeObjcopyOnRawBinary(const CommonConfig &Config,
10481063
// (-B<arch>).
10491064
const ElfType OutputElfType =
10501065
getOutputElfType(Config.OutputArch.value_or(MachineInfo()));
1051-
if (Error E = handleArgs(Config, ELFConfig, OutputElfType, **Obj))
1066+
if (Error E =
1067+
handleArgs(Config, ELFConfig, OutputElfType, **Obj, WarningCallback))
10521068
return E;
10531069
return writeOutput(Config, **Obj, Out, OutputElfType);
10541070
}
10551071

1056-
Error objcopy::elf::executeObjcopyOnBinary(const CommonConfig &Config,
1057-
const ELFConfig &ELFConfig,
1058-
object::ELFObjectFileBase &In,
1059-
raw_ostream &Out) {
1072+
Error objcopy::elf::executeObjcopyOnBinary(
1073+
const CommonConfig &Config, const ELFConfig &ELFConfig,
1074+
object::ELFObjectFileBase &In, raw_ostream &Out,
1075+
function_ref<void(const Twine &)> WarningCallback) {
10601076
ELFReader Reader(&In, Config.ExtractPartition);
10611077
Expected<std::unique_ptr<Object>> Obj =
10621078
Reader.create(!Config.SymbolsToAdd.empty());
@@ -1067,7 +1083,8 @@ Error objcopy::elf::executeObjcopyOnBinary(const CommonConfig &Config,
10671083
? getOutputElfType(*Config.OutputArch)
10681084
: getOutputElfType(In);
10691085

1070-
if (Error E = handleArgs(Config, ELFConfig, OutputElfType, **Obj))
1086+
if (Error E =
1087+
handleArgs(Config, ELFConfig, OutputElfType, **Obj, WarningCallback))
10711088
return createFileError(Config.InputFilename, std::move(E));
10721089

10731090
if (Error E = writeOutput(Config, **Obj, Out, OutputElfType))

llvm/lib/ObjCopy/MachO/MachOObjcopy.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -445,10 +445,10 @@ static Error handleArgs(const CommonConfig &Config,
445445
return Error::success();
446446
}
447447

448-
Error objcopy::macho::executeObjcopyOnBinary(const CommonConfig &Config,
449-
const MachOConfig &MachOConfig,
450-
object::MachOObjectFile &In,
451-
raw_ostream &Out) {
448+
Error objcopy::macho::executeObjcopyOnBinary(
449+
const CommonConfig &Config, const MachOConfig &MachOConfig,
450+
object::MachOObjectFile &In, raw_ostream &Out,
451+
function_ref<void(const Twine &)> WarningCallback) {
452452
MachOReader Reader(In);
453453
Expected<std::unique_ptr<Object>> O = Reader.create();
454454
if (!O)
@@ -484,14 +484,14 @@ Error objcopy::macho::executeObjcopyOnBinary(const CommonConfig &Config,
484484

485485
Error objcopy::macho::executeObjcopyOnMachOUniversalBinary(
486486
const MultiFormatConfig &Config, const MachOUniversalBinary &In,
487-
raw_ostream &Out) {
487+
raw_ostream &Out, function_ref<void(const Twine &)> WarningCallback) {
488488
SmallVector<OwningBinary<Binary>, 2> Binaries;
489489
SmallVector<Slice, 2> Slices;
490490
for (const auto &O : In.objects()) {
491491
Expected<std::unique_ptr<Archive>> ArOrErr = O.getAsArchive();
492492
if (ArOrErr) {
493493
Expected<std::vector<NewArchiveMember>> NewArchiveMembersOrErr =
494-
createNewArchiveMembers(Config, **ArOrErr);
494+
createNewArchiveMembers(Config, **ArOrErr, WarningCallback);
495495
if (!NewArchiveMembersOrErr)
496496
return NewArchiveMembersOrErr.takeError();
497497
auto Kind = (*ArOrErr)->kind();
@@ -542,8 +542,9 @@ Error objcopy::macho::executeObjcopyOnMachOUniversalBinary(
542542
if (!MachO)
543543
return MachO.takeError();
544544

545-
if (Error E = executeObjcopyOnBinary(Config.getCommonConfig(), *MachO,
546-
**ObjOrErr, MemStream))
545+
if (Error E =
546+
executeObjcopyOnBinary(Config.getCommonConfig(), *MachO, **ObjOrErr,
547+
MemStream, WarningCallback))
547548
return E;
548549

549550
auto MB = std::make_unique<SmallVectorMemoryBuffer>(

llvm/lib/ObjCopy/ObjCopy.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,16 @@ using namespace llvm::object;
3333

3434
/// The function executeObjcopyOnBinary does the dispatch based on the format
3535
/// of the input binary (ELF, MachO or COFF).
36-
Error executeObjcopyOnBinary(const MultiFormatConfig &Config,
37-
object::Binary &In, raw_ostream &Out) {
36+
Error executeObjcopyOnBinary(
37+
const MultiFormatConfig &Config, object::Binary &In, raw_ostream &Out,
38+
function_ref<void(const Twine &)> WarningCallback) {
3839
if (auto *ELFBinary = dyn_cast<object::ELFObjectFileBase>(&In)) {
3940
Expected<const ELFConfig &> ELFConfig = Config.getELFConfig();
4041
if (!ELFConfig)
4142
return ELFConfig.takeError();
4243

4344
return elf::executeObjcopyOnBinary(Config.getCommonConfig(), *ELFConfig,
44-
*ELFBinary, Out);
45+
*ELFBinary, Out, WarningCallback);
4546
}
4647
if (auto *COFFBinary = dyn_cast<object::COFFObjectFile>(&In)) {
4748
Expected<const COFFConfig &> COFFConfig = Config.getCOFFConfig();
@@ -57,12 +58,12 @@ Error executeObjcopyOnBinary(const MultiFormatConfig &Config,
5758
return MachOConfig.takeError();
5859

5960
return macho::executeObjcopyOnBinary(Config.getCommonConfig(), *MachOConfig,
60-
*MachOBinary, Out);
61+
*MachOBinary, Out, WarningCallback);
6162
}
6263
if (auto *MachOUniversalBinary =
6364
dyn_cast<object::MachOUniversalBinary>(&In)) {
6465
return macho::executeObjcopyOnMachOUniversalBinary(
65-
Config, *MachOUniversalBinary, Out);
66+
Config, *MachOUniversalBinary, Out, WarningCallback);
6667
}
6768
if (auto *WasmBinary = dyn_cast<object::WasmObjectFile>(&In)) {
6869
Expected<const WasmConfig &> WasmConfig = Config.getWasmConfig();

0 commit comments

Comments
 (0)