lib/buildid.c
Source file repositories/reference/linux-study-clean/lib/buildid.c
File Facts
- System
- Linux kernel
- Corpus path
lib/buildid.c- Extension
.c- Size
- 10657 bytes
- Lines
- 408
- Domain
- Kernel Services
- Bucket
- lib
- Inferred role
- Kernel Services: implementation source
- Status
- source implementation candidate
Why This File Exists
Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/buildid.hlinux/cache.hlinux/elf.hlinux/kernel.hlinux/pagemap.hlinux/fs.hlinux/secretmem.h
Detected Declarations
function freader_init_from_filefunction freader_init_from_memfunction freader_put_foliofunction freader_get_foliofunction freader_cleanupfunction parse_build_idfunction get_build_id_32function get_build_id_64function __build_id_parsefunction build_id_parse_nofaultfunction build_id_parsefunction build_id_parse_filefunction build_id_parse_buffunction init_vmlinux_build_id
Annotated Snippet
if (file_off + sz > r->data_sz) {
r->err = -ERANGE;
return NULL;
}
return r->data + file_off;
}
/* reject secretmem folios created with memfd_secret() */
if (secretmem_mapping(r->file->f_mapping)) {
r->err = -EFAULT;
return NULL;
}
/* use __kernel_read() for sleepable context */
if (r->may_fault) {
ssize_t ret;
ret = __kernel_read(r->file, r->buf, sz, &file_off);
if (ret != sz) {
r->err = (ret < 0) ? ret : -EIO;
return NULL;
}
return r->buf;
}
/* fetch or reuse folio for given file offset */
r->err = freader_get_folio(r, file_off);
if (r->err)
return NULL;
/* if requested data is crossing folio boundaries, we have to copy
* everything into our local buffer to keep a simple linear memory
* access interface
*/
folio_sz = folio_size(r->folio);
if (file_off + sz > r->folio_off + folio_sz) {
u64 part_sz = r->folio_off + folio_sz - file_off, off;
memcpy(r->buf, r->addr + file_off - r->folio_off, part_sz);
off = part_sz;
while (off < sz) {
/* fetch next folio */
r->err = freader_get_folio(r, r->folio_off + folio_sz);
if (r->err)
return NULL;
folio_sz = folio_size(r->folio);
part_sz = min_t(u64, sz - off, folio_sz);
memcpy(r->buf + off, r->addr, part_sz);
off += part_sz;
}
return r->buf;
}
/* if data fits in a single folio, just return direct pointer */
return r->addr + (file_off - r->folio_off);
}
void freader_cleanup(struct freader *r)
{
if (!r->buf)
return; /* non-file-backed mode */
freader_put_folio(r);
}
/*
* Parse build id from the note segment. This logic can be shared between
* 32-bit and 64-bit system, because Elf32_Nhdr and Elf64_Nhdr are
* identical.
*/
static int parse_build_id(struct freader *r, unsigned char *build_id, __u32 *size,
loff_t note_off, Elf32_Word note_size)
{
const char note_name[] = "GNU";
const size_t note_name_sz = sizeof(note_name);
u32 build_id_off, new_off, note_end, name_sz, desc_sz;
const Elf32_Nhdr *nhdr;
const char *data;
if (check_add_overflow(note_off, note_size, ¬e_end))
return -EINVAL;
while (note_end - note_off > sizeof(Elf32_Nhdr) + note_name_sz) {
nhdr = freader_fetch(r, note_off, sizeof(Elf32_Nhdr) + note_name_sz);
if (!nhdr)
return r->err;
name_sz = READ_ONCE(nhdr->n_namesz);
Annotation
- Immediate include surface: `linux/buildid.h`, `linux/cache.h`, `linux/elf.h`, `linux/kernel.h`, `linux/pagemap.h`, `linux/fs.h`, `linux/secretmem.h`.
- Detected declarations: `function freader_init_from_file`, `function freader_init_from_mem`, `function freader_put_folio`, `function freader_get_folio`, `function freader_cleanup`, `function parse_build_id`, `function get_build_id_32`, `function get_build_id_64`, `function __build_id_parse`, `function build_id_parse_nofault`.
- Atlas domain: Kernel Services / lib.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.