Skip to content

Commit 4308c74

Browse files
authored
[BOLT][NFC] Refactor relocation arch selection (llvm#87829)
Convert the relocation routines to switch on architecture and have an explicit unreachable default.
1 parent 662c626 commit 4308c74

File tree

1 file changed

+144
-51
lines changed

1 file changed

+144
-51
lines changed

bolt/lib/Core/Relocation.cpp

Lines changed: 144 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -774,60 +774,95 @@ static bool isPCRelativeRISCV(uint64_t Type) {
774774
}
775775

776776
bool Relocation::isSupported(uint64_t Type) {
777-
if (Arch == Triple::aarch64)
777+
switch (Arch) {
778+
default:
779+
return false;
780+
case Triple::aarch64:
778781
return isSupportedAArch64(Type);
779-
if (Arch == Triple::riscv64)
782+
case Triple::riscv64:
780783
return isSupportedRISCV(Type);
781-
return isSupportedX86(Type);
784+
case Triple::x86_64:
785+
return isSupportedX86(Type);
786+
}
782787
}
783788

784789
size_t Relocation::getSizeForType(uint64_t Type) {
785-
if (Arch == Triple::aarch64)
790+
switch (Arch) {
791+
default:
792+
llvm_unreachable("Unsupported architecture");
793+
case Triple::aarch64:
786794
return getSizeForTypeAArch64(Type);
787-
if (Arch == Triple::riscv64)
795+
case Triple::riscv64:
788796
return getSizeForTypeRISCV(Type);
789-
return getSizeForTypeX86(Type);
797+
case Triple::x86_64:
798+
return getSizeForTypeX86(Type);
799+
}
790800
}
791801

792802
bool Relocation::skipRelocationType(uint64_t Type) {
793-
if (Arch == Triple::aarch64)
803+
switch (Arch) {
804+
default:
805+
llvm_unreachable("Unsupported architecture");
806+
case Triple::aarch64:
794807
return skipRelocationTypeAArch64(Type);
795-
if (Arch == Triple::riscv64)
808+
case Triple::riscv64:
796809
return skipRelocationTypeRISCV(Type);
797-
return skipRelocationTypeX86(Type);
810+
case Triple::x86_64:
811+
return skipRelocationTypeX86(Type);
812+
}
798813
}
799814

800815
bool Relocation::skipRelocationProcess(uint64_t &Type, uint64_t Contents) {
801-
if (Arch == Triple::aarch64)
816+
switch (Arch) {
817+
default:
818+
llvm_unreachable("Unsupported architecture");
819+
case Triple::aarch64:
802820
return skipRelocationProcessAArch64(Type, Contents);
803-
if (Arch == Triple::riscv64)
804-
skipRelocationProcessRISCV(Type, Contents);
805-
return skipRelocationProcessX86(Type, Contents);
821+
case Triple::riscv64:
822+
return skipRelocationProcessRISCV(Type, Contents);
823+
case Triple::x86_64:
824+
return skipRelocationProcessX86(Type, Contents);
825+
}
806826
}
807827

808828
uint64_t Relocation::encodeValue(uint64_t Type, uint64_t Value, uint64_t PC) {
809-
if (Arch == Triple::aarch64)
829+
switch (Arch) {
830+
default:
831+
llvm_unreachable("Unsupported architecture");
832+
case Triple::aarch64:
810833
return encodeValueAArch64(Type, Value, PC);
811-
if (Arch == Triple::riscv64)
834+
case Triple::riscv64:
812835
return encodeValueRISCV(Type, Value, PC);
813-
return encodeValueX86(Type, Value, PC);
836+
case Triple::x86_64:
837+
return encodeValueX86(Type, Value, PC);
838+
}
814839
}
815840

816841
uint64_t Relocation::extractValue(uint64_t Type, uint64_t Contents,
817842
uint64_t PC) {
818-
if (Arch == Triple::aarch64)
843+
switch (Arch) {
844+
default:
845+
llvm_unreachable("Unsupported architecture");
846+
case Triple::aarch64:
819847
return extractValueAArch64(Type, Contents, PC);
820-
if (Arch == Triple::riscv64)
848+
case Triple::riscv64:
821849
return extractValueRISCV(Type, Contents, PC);
822-
return extractValueX86(Type, Contents, PC);
850+
case Triple::x86_64:
851+
return extractValueX86(Type, Contents, PC);
852+
}
823853
}
824854

825855
bool Relocation::isGOT(uint64_t Type) {
826-
if (Arch == Triple::aarch64)
856+
switch (Arch) {
857+
default:
858+
llvm_unreachable("Unsupported architecture");
859+
case Triple::aarch64:
827860
return isGOTAArch64(Type);
828-
if (Arch == Triple::riscv64)
861+
case Triple::riscv64:
829862
return isGOTRISCV(Type);
830-
return isGOTX86(Type);
863+
case Triple::x86_64:
864+
return isGOTX86(Type);
865+
}
831866
}
832867

833868
bool Relocation::isX86GOTPCRELX(uint64_t Type) {
@@ -845,27 +880,42 @@ bool Relocation::isX86GOTPC64(uint64_t Type) {
845880
bool Relocation::isNone(uint64_t Type) { return Type == getNone(); }
846881

847882
bool Relocation::isRelative(uint64_t Type) {
848-
if (Arch == Triple::aarch64)
883+
switch (Arch) {
884+
default:
885+
llvm_unreachable("Unsupported architecture");
886+
case Triple::aarch64:
849887
return Type == ELF::R_AARCH64_RELATIVE;
850-
if (Arch == Triple::riscv64)
888+
case Triple::riscv64:
851889
return Type == ELF::R_RISCV_RELATIVE;
852-
return Type == ELF::R_X86_64_RELATIVE;
890+
case Triple::x86_64:
891+
return Type == ELF::R_X86_64_RELATIVE;
892+
}
853893
}
854894

855895
bool Relocation::isIRelative(uint64_t Type) {
856-
if (Arch == Triple::aarch64)
896+
switch (Arch) {
897+
default:
898+
llvm_unreachable("Unsupported architecture");
899+
case Triple::aarch64:
857900
return Type == ELF::R_AARCH64_IRELATIVE;
858-
if (Arch == Triple::riscv64)
901+
case Triple::riscv64:
859902
llvm_unreachable("not implemented");
860-
return Type == ELF::R_X86_64_IRELATIVE;
903+
case Triple::x86_64:
904+
return Type == ELF::R_X86_64_IRELATIVE;
905+
}
861906
}
862907

863908
bool Relocation::isTLS(uint64_t Type) {
864-
if (Arch == Triple::aarch64)
909+
switch (Arch) {
910+
default:
911+
llvm_unreachable("Unsupported architecture");
912+
case Triple::aarch64:
865913
return isTLSAArch64(Type);
866-
if (Arch == Triple::riscv64)
914+
case Triple::riscv64:
867915
return isTLSRISCV(Type);
868-
return isTLSX86(Type);
916+
case Triple::x86_64:
917+
return isTLSX86(Type);
918+
}
869919
}
870920

871921
bool Relocation::isInstructionReference(uint64_t Type) {
@@ -882,49 +932,81 @@ bool Relocation::isInstructionReference(uint64_t Type) {
882932
}
883933

884934
uint64_t Relocation::getNone() {
885-
if (Arch == Triple::aarch64)
935+
switch (Arch) {
936+
default:
937+
llvm_unreachable("Unsupported architecture");
938+
case Triple::aarch64:
886939
return ELF::R_AARCH64_NONE;
887-
if (Arch == Triple::riscv64)
940+
case Triple::riscv64:
888941
return ELF::R_RISCV_NONE;
889-
return ELF::R_X86_64_NONE;
942+
case Triple::x86_64:
943+
return ELF::R_X86_64_NONE;
944+
}
890945
}
891946

892947
uint64_t Relocation::getPC32() {
893-
if (Arch == Triple::aarch64)
948+
switch (Arch) {
949+
default:
950+
llvm_unreachable("Unsupported architecture");
951+
case Triple::aarch64:
894952
return ELF::R_AARCH64_PREL32;
895-
if (Arch == Triple::riscv64)
953+
case Triple::riscv64:
896954
return ELF::R_RISCV_32_PCREL;
897-
return ELF::R_X86_64_PC32;
955+
case Triple::x86_64:
956+
return ELF::R_X86_64_PC32;
957+
}
898958
}
899959

900960
uint64_t Relocation::getPC64() {
901-
if (Arch == Triple::aarch64)
961+
switch (Arch) {
962+
default:
963+
llvm_unreachable("Unsupported architecture");
964+
case Triple::aarch64:
902965
return ELF::R_AARCH64_PREL64;
903-
if (Arch == Triple::riscv64)
966+
case Triple::riscv64:
904967
llvm_unreachable("not implemented");
905-
return ELF::R_X86_64_PC64;
968+
case Triple::x86_64:
969+
return ELF::R_X86_64_PC64;
970+
}
906971
}
907972

908973
bool Relocation::isPCRelative(uint64_t Type) {
909-
if (Arch == Triple::aarch64)
974+
switch (Arch) {
975+
default:
976+
llvm_unreachable("Unsupported architecture");
977+
case Triple::aarch64:
910978
return isPCRelativeAArch64(Type);
911-
if (Arch == Triple::riscv64)
979+
case Triple::riscv64:
912980
return isPCRelativeRISCV(Type);
913-
return isPCRelativeX86(Type);
981+
case Triple::x86_64:
982+
return isPCRelativeX86(Type);
983+
}
914984
}
915985

916986
uint64_t Relocation::getAbs64() {
917-
if (Arch == Triple::aarch64)
987+
switch (Arch) {
988+
default:
989+
llvm_unreachable("Unsupported architecture");
990+
case Triple::aarch64:
918991
return ELF::R_AARCH64_ABS64;
919-
if (Arch == Triple::riscv64)
992+
case Triple::riscv64:
920993
return ELF::R_RISCV_64;
921-
return ELF::R_X86_64_64;
994+
case Triple::x86_64:
995+
return ELF::R_X86_64_64;
996+
}
922997
}
923998

924999
uint64_t Relocation::getRelative() {
925-
if (Arch == Triple::aarch64)
1000+
switch (Arch) {
1001+
default:
1002+
llvm_unreachable("Unsupported architecture");
1003+
case Triple::aarch64:
9261004
return ELF::R_AARCH64_RELATIVE;
927-
return ELF::R_X86_64_RELATIVE;
1005+
case Triple::riscv64:
1006+
llvm_unreachable("not implemented");
1007+
case Triple::x86_64:
1008+
return ELF::R_X86_64_RELATIVE;
1009+
}
9281010
}
9291011

9301012
size_t Relocation::emit(MCStreamer *Streamer) const {
@@ -991,9 +1073,16 @@ void Relocation::print(raw_ostream &OS) const {
9911073
static const char *AArch64RelocNames[] = {
9921074
#include "llvm/BinaryFormat/ELFRelocs/AArch64.def"
9931075
};
994-
if (Arch == Triple::aarch64)
1076+
switch (Arch) {
1077+
default:
1078+
OS << "RType:" << Twine::utohexstr(Type);
1079+
break;
1080+
1081+
case Triple::aarch64:
9951082
OS << AArch64RelocNames[Type];
996-
else if (Arch == Triple::riscv64) {
1083+
break;
1084+
1085+
case Triple::riscv64:
9971086
// RISC-V relocations are not sequentially numbered so we cannot use an
9981087
// array
9991088
switch (Type) {
@@ -1006,8 +1095,12 @@ void Relocation::print(raw_ostream &OS) const {
10061095
break;
10071096
#include "llvm/BinaryFormat/ELFRelocs/RISCV.def"
10081097
}
1009-
} else
1098+
break;
1099+
1100+
case Triple::x86_64:
10101101
OS << X86RelocNames[Type];
1102+
break;
1103+
}
10111104
OS << ", 0x" << Twine::utohexstr(Offset);
10121105
if (Symbol) {
10131106
OS << ", " << Symbol->getName();

0 commit comments

Comments
 (0)