MCPcopy Create free account
hub / github.com/LPC4/Full-Stack / link

Method link

crates/asm-to-binary/src/object_linker.rs:35–291  ·  view source on GitHub ↗
(modules: &[(&str, &AssembledOutput)])

Source from the content-addressed store, hash-verified

33 // Each module is walked in ELF load order: non-BSS first, BSS last.
34 struct Contrib<'a> {
35 module_idx: usize,
36 sec: &'a SectionData,
37 }
38
39 // The canonical output order: Text -> RoData -> Data -> Custom(...) -> Bss.
40 let all_kinds = collect_ordered_kinds(modules);
41 let mut kind_contribs: Vec<(&SectionKind, Vec<Contrib<'_>>)> =
42 all_kinds.iter().map(|k| (k, Vec::new())).collect();
43
44 for (mi, (_name, module)) in modules.iter().enumerate() {
45 let ordered = ordered_sections(module);
46 for sec in ordered {
47 let Some(kind) = &sec.kind else {
48 continue;
49 };
50 let contrib = Contrib {
51 module_idx: mi,
52 sec,
53 };
54 for (k, contribs) in &mut kind_contribs {
55 if *k == kind {
56 contribs.push(contrib);
57 break;
58 }
59 }
60 }
61 }
62
63 // --- Merge bytes and compute output bases ---
64 // Contributions concatenate in module order; `mod_starts[mi]` is module `mi`'s offset.
65 struct MergedSec {
66 kind: SectionKind,
67 bytes: Vec<u8>,
68 mod_starts: HashMap<usize, u64>,
69 }
70
71 let mut merged_secs: Vec<MergedSec> = Vec::new();
72 // Map: (kind_name, module_idx) -> absolute output base
73 let mut contrib_base: HashMap<(String, usize), u64> = HashMap::new();
74 let mut output_bases_by_kind: HashMap<String, u64> = HashMap::new();
75 let mut running_abs: u64 = 0;
76
77 for (kind, contribs) in &kind_contribs {
78 if contribs.is_empty() {
79 continue;
80 }
81 let mut merged_bytes: Vec<u8> = Vec::new();
82 let mut mod_starts: HashMap<usize, u64> = HashMap::new();
83 let kind_name = kind.name().to_owned();
84
85 output_bases_by_kind.insert(kind_name.clone(), running_abs);
86
87 for c in contribs {
88 let start_in_merged = merged_bytes.len() as u64;
89 mod_starts.insert(c.module_idx, start_in_merged);
90 contrib_base.insert(
91 (kind_name.clone(), c.module_idx),
92 running_abs + start_in_merged,

Callers

nothing calls this directly

Calls 15

collect_ordered_kindsFunction · 0.85
ordered_sectionsFunction · 0.85
build_module_section_mapFunction · 0.85
patch_call_pairFunction · 0.85
patch_jalFunction · 0.85
patch_laFunction · 0.85
collectMethod · 0.80
pushMethod · 0.80
getMethod · 0.80
relocations_iterMethod · 0.80
nameMethod · 0.45

Tested by

no test coverage detected