|
11 | 11 | #include "Symbols.h"
|
12 | 12 | #include "SyntheticSections.h"
|
13 | 13 | #include "Target.h"
|
| 14 | +#include "llvm/Support/ELFAttributes.h" |
| 15 | +#include "llvm/Support/LEB128.h" |
| 16 | +#include "llvm/Support/RISCVAttributeParser.h" |
| 17 | +#include "llvm/Support/RISCVAttributes.h" |
| 18 | +#include "llvm/Support/RISCVISAInfo.h" |
14 | 19 | #include "llvm/Support/TimeProfiler.h"
|
15 | 20 |
|
16 | 21 | using namespace llvm;
|
@@ -816,6 +821,205 @@ void elf::riscvFinalizeRelax(int passes) {
|
816 | 821 | }
|
817 | 822 | }
|
818 | 823 |
|
| 824 | +namespace { |
| 825 | +// Representation of the merged .riscv.attributes input sections. The psABI |
| 826 | +// specifies merge policy for attributes. E.g. if we link an object without an |
| 827 | +// extension with an object with the extension, the output Tag_RISCV_arch shall |
| 828 | +// contain the extension. Some tools like objdump parse .riscv.attributes and |
| 829 | +// disabling some instructions if the first Tag_RISCV_arch does not contain an |
| 830 | +// extension. |
| 831 | +class RISCVAttributesSection final : public SyntheticSection { |
| 832 | +public: |
| 833 | + RISCVAttributesSection() |
| 834 | + : SyntheticSection(0, SHT_RISCV_ATTRIBUTES, 1, ".riscv.attributes") {} |
| 835 | + |
| 836 | + size_t getSize() const override { return size; } |
| 837 | + void writeTo(uint8_t *buf) override; |
| 838 | + |
| 839 | + static constexpr StringRef vendor = "riscv"; |
| 840 | + DenseMap<unsigned, unsigned> intAttr; |
| 841 | + DenseMap<unsigned, StringRef> strAttr; |
| 842 | + size_t size = 0; |
| 843 | +}; |
| 844 | +} // namespace |
| 845 | + |
| 846 | +static void mergeArch(RISCVISAInfo::OrderedExtensionMap &mergedExts, |
| 847 | + unsigned &mergedXlen, const InputSectionBase *sec, |
| 848 | + StringRef s) { |
| 849 | + auto maybeInfo = |
| 850 | + RISCVISAInfo::parseArchString(s, /*EnableExperimentalExtension=*/true, |
| 851 | + /*ExperimentalExtensionVersionCheck=*/true); |
| 852 | + if (!maybeInfo) { |
| 853 | + errorOrWarn(toString(sec) + ": " + s + ": " + |
| 854 | + llvm::toString(maybeInfo.takeError())); |
| 855 | + return; |
| 856 | + } |
| 857 | + |
| 858 | + // Merge extensions. |
| 859 | + RISCVISAInfo &info = **maybeInfo; |
| 860 | + if (mergedExts.empty()) { |
| 861 | + mergedExts = info.getExtensions(); |
| 862 | + mergedXlen = info.getXLen(); |
| 863 | + } else { |
| 864 | + for (const auto &ext : info.getExtensions()) { |
| 865 | + if (auto it = mergedExts.find(ext.first); it != mergedExts.end()) { |
| 866 | + // TODO This is untested because RISCVISAInfo::parseArchString does not |
| 867 | + // accept unsupported versions yet. |
| 868 | + if (std::tie(it->second.MajorVersion, it->second.MinorVersion) >= |
| 869 | + std::tie(ext.second.MajorVersion, ext.second.MinorVersion)) |
| 870 | + continue; |
| 871 | + } |
| 872 | + mergedExts[ext.first] = ext.second; |
| 873 | + } |
| 874 | + } |
| 875 | +} |
| 876 | + |
| 877 | +static RISCVAttributesSection * |
| 878 | +mergeAttributesSection(const SmallVector<InputSectionBase *, 0> §ions) { |
| 879 | + RISCVISAInfo::OrderedExtensionMap exts; |
| 880 | + const InputSectionBase *firstStackAlign = nullptr; |
| 881 | + unsigned firstStackAlignValue = 0, xlen = 0; |
| 882 | + bool hasArch = false; |
| 883 | + |
| 884 | + in.riscvAttributes = std::make_unique<RISCVAttributesSection>(); |
| 885 | + auto &merged = static_cast<RISCVAttributesSection &>(*in.riscvAttributes); |
| 886 | + |
| 887 | + // Collect all tags values from attributes section. |
| 888 | + const auto &attributesTags = RISCVAttrs::getRISCVAttributeTags(); |
| 889 | + for (const InputSectionBase *sec : sections) { |
| 890 | + RISCVAttributeParser parser; |
| 891 | + if (Error e = parser.parse(sec->content(), support::little)) |
| 892 | + warn(toString(sec) + ": " + llvm::toString(std::move(e))); |
| 893 | + for (const auto &tag : attributesTags) { |
| 894 | + switch (RISCVAttrs::AttrType(tag.attr)) { |
| 895 | + // Integer attributes. |
| 896 | + case RISCVAttrs::STACK_ALIGN: |
| 897 | + if (auto i = parser.getAttributeValue(tag.attr)) { |
| 898 | + auto r = merged.intAttr.try_emplace(tag.attr, *i); |
| 899 | + if (r.second) { |
| 900 | + firstStackAlign = sec; |
| 901 | + firstStackAlignValue = *i; |
| 902 | + } else if (r.first->second != *i) { |
| 903 | + errorOrWarn(toString(sec) + " has stack_align=" + Twine(*i) + |
| 904 | + " but " + toString(firstStackAlign) + |
| 905 | + " has stack_align=" + Twine(firstStackAlignValue)); |
| 906 | + } |
| 907 | + } |
| 908 | + continue; |
| 909 | + case RISCVAttrs::UNALIGNED_ACCESS: |
| 910 | + if (auto i = parser.getAttributeValue(tag.attr)) |
| 911 | + merged.intAttr[tag.attr] |= *i; |
| 912 | + continue; |
| 913 | + |
| 914 | + // String attributes. |
| 915 | + case RISCVAttrs::ARCH: |
| 916 | + if (auto s = parser.getAttributeString(tag.attr)) { |
| 917 | + hasArch = true; |
| 918 | + mergeArch(exts, xlen, sec, *s); |
| 919 | + } |
| 920 | + continue; |
| 921 | + |
| 922 | + // Attributes which use the default handling. |
| 923 | + case RISCVAttrs::PRIV_SPEC: |
| 924 | + case RISCVAttrs::PRIV_SPEC_MINOR: |
| 925 | + case RISCVAttrs::PRIV_SPEC_REVISION: |
| 926 | + break; |
| 927 | + } |
| 928 | + |
| 929 | + // Fallback for deprecated priv_spec* and other unknown attributes: retain |
| 930 | + // the attribute if all input sections agree on the value. GNU ld uses 0 |
| 931 | + // and empty strings as default values which are not dumped to the output. |
| 932 | + // TODO Adjust after resolution to |
| 933 | + // https://github.com/riscv-non-isa/riscv-elf-psabi-doc/issues/352 |
| 934 | + if (tag.attr % 2 == 0) { |
| 935 | + if (auto i = parser.getAttributeValue(tag.attr)) { |
| 936 | + auto r = merged.intAttr.try_emplace(tag.attr, *i); |
| 937 | + if (!r.second && r.first->second != *i) |
| 938 | + r.first->second = 0; |
| 939 | + } |
| 940 | + } else if (auto s = parser.getAttributeString(tag.attr)) { |
| 941 | + auto r = merged.strAttr.try_emplace(tag.attr, *s); |
| 942 | + if (!r.second && r.first->second != *s) |
| 943 | + r.first->second = {}; |
| 944 | + } |
| 945 | + } |
| 946 | + } |
| 947 | + |
| 948 | + if (hasArch) { |
| 949 | + if (auto result = RISCVISAInfo::postProcessAndChecking( |
| 950 | + std::make_unique<RISCVISAInfo>(xlen, exts))) { |
| 951 | + merged.strAttr.try_emplace(RISCVAttrs::ARCH, |
| 952 | + saver().save((*result)->toString())); |
| 953 | + } else { |
| 954 | + errorOrWarn(llvm::toString(result.takeError())); |
| 955 | + } |
| 956 | + } |
| 957 | + |
| 958 | + // The total size of headers: format-version [ <section-length> "vendor-name" |
| 959 | + // [ <file-tag> <size>. |
| 960 | + size_t size = 5 + merged.vendor.size() + 1 + 5; |
| 961 | + for (auto &attr : merged.intAttr) |
| 962 | + if (attr.second != 0) |
| 963 | + size += getULEB128Size(attr.first) + getULEB128Size(attr.second); |
| 964 | + for (auto &attr : merged.strAttr) |
| 965 | + if (!attr.second.empty()) |
| 966 | + size += getULEB128Size(attr.first) + attr.second.size() + 1; |
| 967 | + merged.size = size; |
| 968 | + return &merged; |
| 969 | +} |
| 970 | + |
| 971 | +void RISCVAttributesSection::writeTo(uint8_t *buf) { |
| 972 | + const size_t size = getSize(); |
| 973 | + uint8_t *const end = buf + size; |
| 974 | + *buf = ELFAttrs::Format_Version; |
| 975 | + write32(buf + 1, size - 1); |
| 976 | + buf += 5; |
| 977 | + |
| 978 | + memcpy(buf, vendor.data(), vendor.size()); |
| 979 | + buf += vendor.size() + 1; |
| 980 | + |
| 981 | + *buf = ELFAttrs::File; |
| 982 | + write32(buf + 1, end - buf); |
| 983 | + buf += 5; |
| 984 | + |
| 985 | + for (auto &attr : intAttr) { |
| 986 | + if (attr.second == 0) |
| 987 | + continue; |
| 988 | + buf += encodeULEB128(attr.first, buf); |
| 989 | + buf += encodeULEB128(attr.second, buf); |
| 990 | + } |
| 991 | + for (auto &attr : strAttr) { |
| 992 | + if (attr.second.empty()) |
| 993 | + continue; |
| 994 | + buf += encodeULEB128(attr.first, buf); |
| 995 | + memcpy(buf, attr.second.data(), attr.second.size()); |
| 996 | + buf += attr.second.size() + 1; |
| 997 | + } |
| 998 | +} |
| 999 | + |
| 1000 | +void elf::mergeRISCVAttributesSections() { |
| 1001 | + // Find the first input SHT_RISCV_ATTRIBUTES; return if not found. |
| 1002 | + size_t place = |
| 1003 | + llvm::find_if(ctx.inputSections, |
| 1004 | + [](auto *s) { return s->type == SHT_RISCV_ATTRIBUTES; }) - |
| 1005 | + ctx.inputSections.begin(); |
| 1006 | + if (place == ctx.inputSections.size()) |
| 1007 | + return; |
| 1008 | + |
| 1009 | + // Extract all SHT_RISCV_ATTRIBUTES sections into `sections`. |
| 1010 | + SmallVector<InputSectionBase *, 0> sections; |
| 1011 | + llvm::erase_if(ctx.inputSections, [&](InputSectionBase *s) { |
| 1012 | + if (s->type != SHT_RISCV_ATTRIBUTES) |
| 1013 | + return false; |
| 1014 | + sections.push_back(s); |
| 1015 | + return true; |
| 1016 | + }); |
| 1017 | + |
| 1018 | + // Add the merged section. |
| 1019 | + ctx.inputSections.insert(ctx.inputSections.begin() + place, |
| 1020 | + mergeAttributesSection(sections)); |
| 1021 | +} |
| 1022 | + |
819 | 1023 | TargetInfo *elf::getRISCVTargetInfo() {
|
820 | 1024 | static RISCV target;
|
821 | 1025 | return ⌖
|
|
0 commit comments