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.

Dependency Surface

Detected Declarations

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, &note_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

Implementation Notes