Produce a relocatable ELF-64 object (`ET_REL`) suitable for external linking.
(&self, object_name: &str)
| 514 | // ---- Emit section headers ---- |
| 515 | debug_assert_eq!(buf.len() as u64, shdrs_offset); |
| 516 | |
| 517 | // SHT_NULL |
| 518 | buf.extend_from_slice(&[0u8; 64]); |
| 519 | |
| 520 | // One header per program section |
| 521 | for (i, es) in elf_secs.iter().enumerate() { |
| 522 | let filesz = if matches!(es.sec.kind, Some(SectionKind::Bss)) { |
| 523 | 0u64 |
| 524 | } else { |
| 525 | es.sec.bytes.len() as u64 |
| 526 | }; |
| 527 | push_u32_le(&mut buf, shstrtab_indices[i]); // sh_name |
| 528 | push_u32_le(&mut buf, es.sh_type); // sh_type |
| 529 | push_u64_le(&mut buf, es.sh_flags); // sh_flags |
| 530 | push_u64_le(&mut buf, es.load_addr); // sh_addr |
| 531 | push_u64_le(&mut buf, sec_file_offsets[i]); // sh_offset |
| 532 | push_u64_le(&mut buf, filesz); // sh_size |
| 533 | push_u32_le(&mut buf, 0); // sh_link |
| 534 | push_u32_le(&mut buf, 0); // sh_info |
| 535 | push_u64_le(&mut buf, 4); // sh_addralign |
| 536 | push_u64_le(&mut buf, 0); // sh_entsize |
| 537 | } |
| 538 | |
| 539 | // .shstrtab header |
| 540 | push_u32_le(&mut buf, shstrtab_name_idx); |
| 541 | push_u32_le(&mut buf, SHT_STRTAB); |
| 542 | push_u64_le(&mut buf, 0); |
| 543 | push_u64_le(&mut buf, 0); |
| 544 | push_u64_le(&mut buf, shstrtab_offset); |
| 545 | push_u64_le(&mut buf, shstrtab.len() as u64); |
| 546 | push_u32_le(&mut buf, 0); |
| 547 | push_u32_le(&mut buf, 0); |
| 548 | push_u64_le(&mut buf, 1); |
| 549 | push_u64_le(&mut buf, 0); |
| 550 | |
| 551 | // .strtab header |
| 552 | push_u32_le(&mut buf, strtab_name_idx); |
| 553 | push_u32_le(&mut buf, SHT_STRTAB); |
| 554 | push_u64_le(&mut buf, 0); |
| 555 | push_u64_le(&mut buf, 0); |
| 556 | push_u64_le(&mut buf, strtab_offset); |
| 557 | push_u64_le(&mut buf, strtab.len() as u64); |
| 558 | push_u32_le(&mut buf, 0); |
| 559 | push_u32_le(&mut buf, 0); |
| 560 | push_u64_le(&mut buf, 1); |
| 561 | push_u64_le(&mut buf, 0); |
| 562 | |
| 563 | // .symtab header |
| 564 | let strtab_shndx = 1 + n_prog_secs + 1; // index of .strtab |
| 565 | let n_local_syms = sorted_syms |
| 566 | .iter() |
| 567 | .filter(|(n, _)| !self.global_symbols.contains(n)) |
| 568 | .count() |
| 569 | + 1; // +1 for STN_UNDEF |
| 570 | push_u32_le(&mut buf, symtab_name_idx); |
| 571 | push_u32_le(&mut buf, SHT_SYMTAB); |
| 572 | push_u64_le(&mut buf, 0); |
| 573 | push_u64_le(&mut buf, 0); |