#include "CopyConfig.h"
-#include "llvm/ADT/BitmaskEnum.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
StripOptTable() : OptTable(StripInfoTable) {}
};
-enum SectionFlag {
- SecNone = 0,
- SecAlloc = 1 << 0,
- SecLoad = 1 << 1,
- SecNoload = 1 << 2,
- SecReadonly = 1 << 3,
- SecDebug = 1 << 4,
- SecCode = 1 << 5,
- SecData = 1 << 6,
- SecRom = 1 << 7,
- SecMerge = 1 << 8,
- SecStrings = 1 << 9,
- SecContents = 1 << 10,
- SecShare = 1 << 11,
- LLVM_MARK_AS_BITMASK_ENUM(/* LargestValue = */ SecShare)
-};
-
} // namespace
static SectionFlag parseSectionRenameFlag(StringRef SectionName) {
.Default(SectionFlag::SecNone);
}
-static Expected<uint64_t>
+static Expected<SectionFlag>
parseSectionFlagSet(ArrayRef<StringRef> SectionFlags) {
SectionFlag ParsedFlags = SectionFlag::SecNone;
for (StringRef Flag : SectionFlags) {
ParsedFlags |= ParsedFlag;
}
- uint64_t NewFlags = 0;
- if (ParsedFlags & SectionFlag::SecAlloc)
- NewFlags |= ELF::SHF_ALLOC;
- if (!(ParsedFlags & SectionFlag::SecReadonly))
- NewFlags |= ELF::SHF_WRITE;
- if (ParsedFlags & SectionFlag::SecCode)
- NewFlags |= ELF::SHF_EXECINSTR;
- if (ParsedFlags & SectionFlag::SecMerge)
- NewFlags |= ELF::SHF_MERGE;
- if (ParsedFlags & SectionFlag::SecStrings)
- NewFlags |= ELF::SHF_STRINGS;
- return NewFlags;
+ return ParsedFlags;
}
static Expected<SectionRename> parseRenameSectionValue(StringRef FlagValue) {
SR.NewName = NameAndFlags[0];
if (NameAndFlags.size() > 1) {
- Expected<uint64_t> ParsedFlagSet =
+ Expected<SectionFlag> ParsedFlagSet =
parseSectionFlagSet(makeArrayRef(NameAndFlags).drop_front());
if (!ParsedFlagSet)
return ParsedFlagSet.takeError();
// Flags split: "f1" "f2" ...
SmallVector<StringRef, 6> SectionFlags;
Section2Flags.second.split(SectionFlags, ',');
- Expected<uint64_t> ParsedFlagSet = parseSectionFlagSet(SectionFlags);
+ Expected<SectionFlag> ParsedFlagSet = parseSectionFlagSet(SectionFlags);
if (!ParsedFlagSet)
return ParsedFlagSet.takeError();
SFU.NewFlags = *ParsedFlagSet;
#define LLVM_TOOLS_LLVM_OBJCOPY_COPY_CONFIG_H
#include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/BitmaskEnum.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringMap.h"
bool IsLittleEndian;
};
+// Flags set by --set-section-flags or --rename-section. Interpretation of these
+// is format-specific and not all flags are meaningful for all object file
+// formats. This is a bitmask; many section flags may be set.
+enum SectionFlag {
+ SecNone = 0,
+ SecAlloc = 1 << 0,
+ SecLoad = 1 << 1,
+ SecNoload = 1 << 2,
+ SecReadonly = 1 << 3,
+ SecDebug = 1 << 4,
+ SecCode = 1 << 5,
+ SecData = 1 << 6,
+ SecRom = 1 << 7,
+ SecMerge = 1 << 8,
+ SecStrings = 1 << 9,
+ SecContents = 1 << 10,
+ SecShare = 1 << 11,
+ LLVM_MARK_AS_BITMASK_ENUM(/* LargestValue = */ SecShare)
+};
+
struct SectionRename {
StringRef OriginalName;
StringRef NewName;
- Optional<uint64_t> NewFlags;
+ Optional<SectionFlag> NewFlags;
};
struct SectionFlagsUpdate {
StringRef Name;
- uint64_t NewFlags;
+ SectionFlag NewFlags;
};
enum class DiscardType {
return !isDWOSection(Sec);
}
+uint64_t getNewShfFlags(SectionFlag AllFlags) {
+ uint64_t NewFlags = 0;
+ if (AllFlags & SectionFlag::SecAlloc)
+ NewFlags |= ELF::SHF_ALLOC;
+ if (!(AllFlags & SectionFlag::SecReadonly))
+ NewFlags |= ELF::SHF_WRITE;
+ if (AllFlags & SectionFlag::SecCode)
+ NewFlags |= ELF::SHF_EXECINSTR;
+ if (AllFlags & SectionFlag::SecMerge)
+ NewFlags |= ELF::SHF_MERGE;
+ if (AllFlags & SectionFlag::SecStrings)
+ NewFlags |= ELF::SHF_STRINGS;
+ return NewFlags;
+}
+
static uint64_t setSectionFlagsPreserveMask(uint64_t OldFlags,
uint64_t NewFlags) {
// Preserve some flags which should not be dropped when setting flags.
const SectionRename &SR = Iter->second;
Sec.Name = SR.NewName;
if (SR.NewFlags.hasValue())
- Sec.Flags =
- setSectionFlagsPreserveMask(Sec.Flags, SR.NewFlags.getValue());
+ Sec.Flags = setSectionFlagsPreserveMask(
+ Sec.Flags, getNewShfFlags(SR.NewFlags.getValue()));
}
}
}
const auto Iter = Config.SetSectionFlags.find(Sec.Name);
if (Iter != Config.SetSectionFlags.end()) {
const SectionFlagsUpdate &SFU = Iter->second;
- Sec.Flags = setSectionFlagsPreserveMask(Sec.Flags, SFU.NewFlags);
+ Sec.Flags = setSectionFlagsPreserveMask(Sec.Flags,
+ getNewShfFlags(SFU.NewFlags));
}
}
}
-
+
for (const auto &Flag : Config.AddSection) {
std::pair<StringRef, StringRef> SecPair = Flag.split("=");
StringRef SecName = SecPair.first;