Keep the mergeable record's chunk in chunks/mergeable_record.rs

MergeableRecordSection, the LC_ATOM_INFO chunk of a -make_mergeable
dylib, and its copy_buf were make_mergeable's, the only chunk defined
outside chunks/. The chunk is chunks/mergeable_record.rs's now, as
mold's chunks/mod.rs holds the .gdb_index chunk; make_mergeable still
builds its contents, as mold's gdb_index.rs builds that chunk's.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Rui UeyamaandClaude Opus 5.5 committed 2026-10-08 18:37:02 +09:00
1 parent e2903c0e98
commit d980bc4b1e
4 files changed
+59 -51

No files matched your search

+52
View File
@@ -0,0 +1,52 @@
//! LC_ATOM_INFO: the mergeable record of a -make_mergeable dylib, which
//! make_mergeable builds (see there).
use crate::arch::Target;
use crate::chunks::ChunkHeader;
use crate::context::Context;
use crate::mergeable::header;
/// LC_ATOM_INFO's data in __LINKEDIT: the record, but for what depends
/// on where it lands in the file, filled in as it is copied out.
#[derive(Debug)]
pub struct MergeableRecordSection {
pub hdr: ChunkHeader,
/// The record, with the content offsets of the entries whose bytes
/// are the image's left to fill.
pub contents: Vec<u8>,
/// Where in the record each such offset goes, and the file offset
/// of the bytes it points at.
pub image_contents: Vec<(u32, u64)>,
/// The content pool's offset in the record.
pub pool_offset: u32,
}
impl MergeableRecordSection {
pub fn new() -> Self {
let mut hdr = ChunkHeader::linkedit();
hdr.p2align = 3;
Self { hdr, contents: Vec::new(), image_contents: Vec::new(), pool_offset: 0 }
}
}
impl Default for MergeableRecordSection {
fn default() -> Self {
Self::new()
}
}
/// Copies the record out, pointing its entries at their bytes now that
/// the record has its place: by offsets back from the content pool, and
/// the whole file by one back from the record.
pub fn copy_buf<E: Target>(ctx: &Context<E>, buf: &mut [u8]) {
let sec = &ctx.mergeable_record;
buf[..sec.contents.len()].copy_from_slice(&sec.contents);
let pool = sec.hdr.fileoff as i64 + sec.pool_offset as i64;
for &(at, fileoff) in &sec.image_contents {
let off = (fileoff as i64 - pool) as i32;
buf[at as usize..at as usize + 4].copy_from_slice(&off.to_le_bytes());
}
let image = header::IMAGE;
buf[image..image + 4].copy_from_slice(&(-(sec.hdr.fileoff as i32)).to_le_bytes());
buf[image + 4..image + 8].copy_from_slice(&(ctx.output_size as u32).to_le_bytes());
}
+2 -1
View File
@@ -28,6 +28,7 @@ pub mod lazy_load_got;
pub mod lazy_load_info;
pub mod lazy_ptrs;
pub mod local_relocs;
pub mod mergeable_record;
pub mod objc_imageinfo;
pub mod objc_methlist;
pub mod objc_stubs;
@@ -372,7 +373,7 @@ pub fn copy_buf<E: Target>(ctx: &Context<E>, id: ChunkId, buf: &mut [u8]) {
ChunkId::ExportTrie => copy_contents(&ctx.export_trie.contents, buf),
ChunkId::FunctionStarts => copy_contents(&ctx.function_starts.contents, buf),
ChunkId::DataInCode => data_in_code::copy_buf(ctx, buf),
ChunkId::MergeableRecord => crate::make_mergeable::copy_buf(ctx, buf),
ChunkId::MergeableRecord => mergeable_record::copy_buf(ctx, buf),
ChunkId::SplitInfo => copy_contents(&ctx.split_info.contents, buf),
ChunkId::LazyLoadInfo => lazy_load_info::copy_buf(ctx, buf),
ChunkId::IndirectSymtab => indirect_symtab::copy_buf(ctx, buf),
+2 -2
View File
@@ -184,7 +184,7 @@ pub struct Context<E: Target> {
pub export_trie: ExportTrieSection,
pub function_starts: FunctionStartsSection,
pub data_in_code: DataInCodeSection,
pub mergeable_record: crate::make_mergeable::MergeableRecordSection,
pub mergeable_record: crate::chunks::mergeable_record::MergeableRecordSection,
pub split_info: SplitInfoSection,
pub lazy_load_info: LazyLoadInfoSection,
pub local_relocs: LocalRelocsSection,
@@ -307,7 +307,7 @@ impl<E: Target> Context<E> {
export_trie: ExportTrieSection::new(),
function_starts: FunctionStartsSection::new(),
data_in_code: DataInCodeSection::new(),
mergeable_record: crate::make_mergeable::MergeableRecordSection::new(),
mergeable_record: crate::chunks::mergeable_record::MergeableRecordSection::new(),
split_info: SplitInfoSection::new(),
lazy_load_info: LazyLoadInfoSection::new(),
local_relocs: LocalRelocsSection::new(),
+3 -48
View File
@@ -23,7 +23,6 @@
use hashbrown::HashMap;
use crate::arch::Target;
use crate::chunks::ChunkHeader;
use crate::context::Context;
use crate::error::RawPath;
use crate::fatal;
@@ -31,59 +30,15 @@ use crate::input_files::{FileId, ObjectFile};
use crate::input_sections::{InputSection, NO_REPLACEMENT, Reloc, RelocTarget};
use crate::macho::*;
use crate::mergeable::{
CustomSection, Entry, Fixup, ctype, fk, header, kind, scope, standard_content_type,
CustomSection, Entry, Fixup, ctype, fk, kind, scope, standard_content_type,
};
use crate::symbol::SymbolId;
mod objc;
mod write;
/// LC_ATOM_INFO's data in __LINKEDIT: the record, but for what depends
/// on where it lands in the file, filled in as it is copied out.
#[derive(Debug)]
pub struct MergeableRecordSection {
pub hdr: ChunkHeader,
/// The record, with the content offsets of the entries whose bytes
/// are the image's left to fill.
pub contents: Vec<u8>,
/// Where in the record each such offset goes, and the file offset
/// of the bytes it points at.
pub image_contents: Vec<(u32, u64)>,
/// The content pool's offset in the record.
pub pool_offset: u32,
}
impl MergeableRecordSection {
pub fn new() -> Self {
let mut hdr = ChunkHeader::linkedit();
hdr.p2align = 3;
Self { hdr, contents: Vec::new(), image_contents: Vec::new(), pool_offset: 0 }
}
}
impl Default for MergeableRecordSection {
fn default() -> Self {
Self::new()
}
}
/// Copies the record out, pointing its entries at their bytes now that
/// the record has its place: by offsets back from the content pool, and
/// the whole file by one back from the record.
pub fn copy_buf<E: Target>(ctx: &Context<E>, buf: &mut [u8]) {
let sec = &ctx.mergeable_record;
buf[..sec.contents.len()].copy_from_slice(&sec.contents);
let pool = sec.hdr.fileoff as i64 + sec.pool_offset as i64;
for &(at, fileoff) in &sec.image_contents {
let off = (fileoff as i64 - pool) as i32;
buf[at as usize..at as usize + 4].copy_from_slice(&off.to_le_bytes());
}
let image = header::IMAGE;
buf[image..image + 4].copy_from_slice(&(-(sec.hdr.fileoff as i32)).to_le_bytes());
buf[image + 4..image + 8].copy_from_slice(&(ctx.output_size as u32).to_le_bytes());
}
/// Builds the record, once the sections have their places in the file.
/// Builds the record, once the sections have their places in the file,
/// into its chunk (see chunks::mergeable_record), which copies it out.
pub fn construct<E: Target>(ctx: &mut Context<E>) {
let mut b = Builder::new(ctx);
b.add_object_entries();