aboutsummaryrefslogtreecommitdiff
path: root/backend/lib/MC/ELFObjectWriter.cpp
diff options
context:
space:
mode:
authorRichard-Qin-X <richard.qin.001@gmail.com>2026-08-28 23:56:19 +0800
committerRichard-Qin-X <richard.qin.001@gmail.com>2026-08-28 23:56:19 +0800
commit7ee5d1913c7cfd0261f86b0ff7be6968b8c07d84 (patch)
tree446dafb95e2f792fee922602b74709c5b9e0777c /backend/lib/MC/ELFObjectWriter.cpp
parentf0cfb240d80cb9f4444fb996350c1f88bc69a142 (diff)
downloadrcc-project-7ee5d1913c7cfd0261f86b0ff7be6968b8c07d84.tar.gz
rcc-project-7ee5d1913c7cfd0261f86b0ff7be6968b8c07d84.tar.xz
[Support] Use standard power-of-two checks
Diffstat (limited to 'backend/lib/MC/ELFObjectWriter.cpp')
-rw-r--r--backend/lib/MC/ELFObjectWriter.cpp13
1 files changed, 5 insertions, 8 deletions
diff --git a/backend/lib/MC/ELFObjectWriter.cpp b/backend/lib/MC/ELFObjectWriter.cpp
index 730ef72..96ec234 100644
--- a/backend/lib/MC/ELFObjectWriter.cpp
+++ b/backend/lib/MC/ELFObjectWriter.cpp
@@ -3,6 +3,7 @@
#include "rcc/MC/ELFObjectWriter.h"
#include <algorithm>
+#include <bit>
#include <cstddef>
#include <cstdint>
#include <limits>
@@ -98,10 +99,6 @@ struct OutputSection final {
MCSymbolRef GroupSignature;
};
-[[nodiscard]] bool isPowerOfTwo(std::uint64_t Value) noexcept {
- return Value != 0 && (Value & (Value - 1U)) == 0;
-}
-
[[nodiscard]] bool isWriterManagedSectionType(std::uint32_t Type) noexcept {
switch (Type) {
case SHTNull:
@@ -127,7 +124,7 @@ struct OutputSection final {
}
[[nodiscard]] Expected<std::uint64_t> alignTo(std::uint64_t Value, std::uint64_t Alignment) {
- if (!isPowerOfTwo(Alignment))
+ if (!std::has_single_bit(Alignment))
return makeUnexpected(ErrorCode::ObjectWritingFailed, "ELF alignment is not a power of two");
auto Adjusted = checkedAdd(Value, Alignment - 1U, "ELF file offset overflows");
if (!Adjusted)
@@ -290,7 +287,7 @@ ELF64LEObjectWriter::writeObject(const MCAssembledObject &Object) const {
return std::unexpected(DescriptionVerification.error());
const bool NoBits =
Description.Kind == MCSectionKind::BSS || Description.Kind == MCSectionKind::TLSBSS;
- if (!isPowerOfTwo(Input.Alignment) || Input.Alignment < Description.Alignment) {
+ if (!std::has_single_bit(Input.Alignment) || Input.Alignment < Description.Alignment) {
return makeUnexpected(ErrorCode::ObjectWritingFailed,
"ELF input section has an invalid effective alignment");
}
@@ -324,7 +321,7 @@ ELF64LEObjectWriter::writeObject(const MCAssembledObject &Object) const {
for (const ELFExtraSection &Extra : Target.ExtraSections) {
if (Extra.Name.empty() || isWriterManagedSectionType(Extra.Type) ||
- !isPowerOfTwo(Extra.Alignment) || (Extra.Flags & (SHFInfoLink | SHFGroup)) != 0 ||
+ !std::has_single_bit(Extra.Alignment) || (Extra.Flags & (SHFInfoLink | SHFGroup)) != 0 ||
(Extra.EntrySize != 0 && Extra.Contents.size() % Extra.EntrySize != 0)) {
return makeUnexpected(ErrorCode::ObjectWritingFailed,
"ELF extra section has an invalid description");
@@ -365,7 +362,7 @@ ELF64LEObjectWriter::writeObject(const MCAssembledObject &Object) const {
std::uint16_t Section = SHNUndefined;
std::uint64_t Value = Input.Value;
if (Input.CommonAlignment != 0) {
- if (!isPowerOfTwo(Input.CommonAlignment) || Input.Size == 0 || Input.SectionIndex ||
+ if (!std::has_single_bit(Input.CommonAlignment) || Input.Size == 0 || Input.SectionIndex ||
Input.Binding == MCObjectSymbolBinding::Local || Input.Value != Input.CommonAlignment) {
return makeUnexpected(ErrorCode::ObjectWritingFailed,
"ELF common symbol has an invalid definition");