162 struct SectionFlags {
167 bool has_relocs =
true;
177 std::array<SectionFlags, unsigned(SectionKind::Max)> section_flags;
181 const TargetInfo &target_info;
183 util::BumpAllocator<> section_allocator;
184 util::SmallVector<util::BumpAllocUniquePtr<DataSection>, 16> sections;
186 std::array<SecRef, unsigned(SectionKind::Max)> default_sections;
188 Assembler(
const TargetInfo &target_info) : target_info(target_info) {}
189 virtual ~Assembler();
192 virtual void reset();
197 DataSection &get_section(SecRef ref) {
199 return *sections[ref.id()];
202 const DataSection &get_section(SecRef ref)
const {
204 return *sections[ref.id()];
207 SecRef create_section(
const TargetInfo::SectionFlags &flags);
209 SecRef create_section(SectionKind kind) {
210 return create_section(target_info.section_flags[
unsigned(kind)]);
213 SecRef get_default_section(SectionKind kind) {
214 SecRef &res = default_sections[unsigned(kind)];
216 res = create_section(kind);
221 virtual void rename_section(SecRef, std::string_view name) = 0;
223 virtual SymRef section_symbol(SecRef) = 0;
227 virtual SymRef sym_add_undef(std::string_view,
SymBinding) = 0;
228 virtual SymRef sym_predef_func(std::string_view,
SymBinding) = 0;
229 virtual SymRef sym_predef_data(std::string_view,
SymBinding) = 0;
230 virtual SymRef sym_predef_tls(std::string_view,
SymBinding) = 0;
232 virtual void sym_def(SymRef, SecRef, u64 pos, u64 size) = 0;
239 SecRef sec, SymRef sym, std::span<const u8> data, u32 align, u32 *off);
241 [[nodiscard]] SymRef sym_def_data(SecRef sec,
242 std::string_view name,
243 std::span<const u8> data,
246 u32 *off =
nullptr) {
247 SymRef sym = sym_predef_data(name, binding);
254 SecRef sec_ref, SymRef sym_ref, u32 size, u32 align, u32 *off =
nullptr);
261 void reloc_sec(SecRef sec, SymRef sym, u32 type, u32 offset, i64 addend) {
262 assert(i32(addend) == addend &&
"non-32-bit addends are unsupported");
263 get_section(sec).relocs.emplace_back(offset, sym, type, addend);
266 void reloc_pc32(SecRef sec, SymRef sym, u32 offset, i64 addend) {
267 reloc_sec(sec, sym, target_info.reloc_pc32, offset, addend);
270 void reloc_abs(SecRef sec, SymRef sym, u32 offset, i64 addend) {
271 reloc_sec(sec, sym, target_info.reloc_abs64, offset, addend);
276 virtual void finalize() {}
278 virtual std::vector<u8> build_object_file() = 0;