commit ce2fa5603023f1257fb6c4f3e2b0dda74e9284d9
parent 318cec683c2afd029cab26deaae8eab8bc0aff9a
Author: xplshn <anto@xplshn.com.ar>
Date: Sun, 22 Nov 2026 11:51:45 -0300
as, ld: note sections get a PT_NOTE segment in the output
Diffstat:
5 files changed, 42 insertions(+), 8 deletions(-)
diff --git a/cmd/dev/as/emit_elf.c b/cmd/dev/as/emit_elf.c
@@ -545,6 +545,8 @@ construct_section_headers(struct Work *work, uint64_t offset)
Elf64_Word type;
if (section->flag & SF_BSS)
type = SHT_NOBITS;
+ else if (section->flag & SF_NOTE)
+ type = SHT_NOTE;
else if (equal_name(section->name, init_array_name))
type = SHT_INIT_ARRAY;
else if (equal_name(section->name, fini_array_name))
diff --git a/cmd/dev/as/parse_asm.c b/cmd/dev/as/parse_asm.c
@@ -1194,14 +1194,18 @@ dir_section(struct ParseInfo *info, enum DirectiveType dir)
/* optional third field, elf section type (@progbits, @nobits, @note,
* ...): this assembler infers the section type from the section
- * name/flags itself, so the field is accepted and discarded rather
- * than fed into set_current_section below */
+ * name/flags itself, so the field is accepted and only @note, which
+ * the name .note* implies as well, changes anything */
+ if (name->bytes >= 5 && !strncmp(name->chars, ".note", 5))
+ flag |= SF_NOTE;
p = skip_whitespaces(info->p);
if (*p == ',') {
const char *q = p + 1;
q = skip_whitespaces(q);
if (*q == '@') {
++q;
+ if (!strncmp(q, "note", 4) && !isalnum((unsigned char)q[4]))
+ flag |= SF_NOTE;
while (isalpha((unsigned char)*q) || isdigit((unsigned char)*q))
++q;
info->p = q;
diff --git a/cmd/dev/as/parse_asm.h b/cmd/dev/as/parse_asm.h
@@ -74,6 +74,7 @@ enum DirectiveType {
#define SF_TLS (1 << 3)
#define SF_INIT_FUNCS (1 << 8)
#define SF_CSTRLITERALS (1 << 9)
+#define SF_NOTE (1 << 10)
struct SectionInfo {
const struct Name *name;
diff --git a/cmd/dev/ld/ld.c b/cmd/dev/ld/ld.c
@@ -361,7 +361,8 @@ outelf_bss(struct outelf *oe)
#define SEC_TLS(s) ((s)->sh_flags & SHF_TLS)
#define SEC_CODE(s) (!SEC_TLS(s) && ((s)->sh_flags & SHF_EXECINSTR))
#define SEC_BSS(s) (!SEC_TLS(s) && (s)->sh_type == SHT_NOBITS)
-#define SEC_DATA(s) (!SEC_TLS(s) && !SEC_CODE(s) && !SEC_BSS(s))
+#define SEC_NOTE(s) (!SEC_TLS(s) && !SEC_CODE(s) && (s)->sh_type == SHT_NOTE)
+#define SEC_DATA(s) (!SEC_TLS(s) && !SEC_CODE(s) && !SEC_BSS(s) && !SEC_NOTE(s))
#define SEC_TDATA(s) (SEC_TLS(s) && (s)->sh_type != SHT_NOBITS)
#define SEC_TBSS(s) (SEC_TLS(s) && (s)->sh_type == SHT_NOBITS)
@@ -610,13 +611,15 @@ link_cs(
/* .init/.fini of all objects are fragments of one function (crti.o
* prologue, object parts, crtn.o epilogue) and must be byte-adjacent
* wherever the objects sit in link order. pass 0 lays out every .init,
- * pass 1 every .fini, pass 2 the rest */
- for (pass = 0; pass < 3; pass++) {
+ * pass 1 every .fini, pass 2 the rest, pass 3 the notes: read only like
+ * the code, and one run of them for the PT_NOTE segment to cover */
+ oe->note_len = 0;
+ for (pass = 0; pass < 4; pass++) {
for (i = 0; i < oe->nsecs; i++) {
struct secmap *sec = &oe->secs[i];
const char *name;
int alignment, is_init, is_fini;
- if (!SEC_CODE(sec->o_shdr))
+ if (pass == 3 ? !SEC_NOTE(sec->o_shdr) : !SEC_CODE(sec->o_shdr))
continue;
name = sec->obj->shstr + sec->o_shdr->sh_name;
is_init = !strcmp(name, ".init");
@@ -628,7 +631,13 @@ link_cs(
len = ALIGN(vaddr + len, alignment) - vaddr;
sec->vaddr = vaddr + len;
sec->faddr = faddr + len;
+ if (pass == 3 && !oe->note_len) {
+ oe->note_vaddr = sec->vaddr;
+ oe->note_faddr = sec->faddr;
+ }
len += sec->o_shdr->sh_size;
+ if (pass == 3)
+ oe->note_len = sec->vaddr + sec->o_shdr->sh_size - oe->note_vaddr;
}
}
phdr->p_type = PT_LOAD;
@@ -790,7 +799,19 @@ outelf_link(struct outelf *oe)
oe->bss_vaddr = vaddr;
len = link_bss(oe, &oe->phdr[2], faddr, vaddr, oe->bss_len);
- oe->nph = has_tls ? 4 : 3;
+ oe->nph = has_tls ? 4 : 3;
+ if (oe->note_len) {
+ Elf_Phdr *note = &oe->phdr[oe->nph++];
+
+ note->p_type = PT_NOTE;
+ note->p_flags = PF_R;
+ note->p_vaddr = oe->note_vaddr;
+ note->p_paddr = oe->note_vaddr;
+ note->p_offset = oe->note_faddr;
+ note->p_filesz = oe->note_len;
+ note->p_memsz = oe->note_len;
+ note->p_align = 4;
+ }
oe->shdr_faddr = faddr;
outelf_reloc(oe);
}
diff --git a/cmd/dev/ld/ld.h b/cmd/dev/ld/ld.h
@@ -21,7 +21,7 @@
#define PAGE_SIZE (1 << 12)
#define PAGE_MASK (PAGE_SIZE - 1)
#define MAXFILES (1 << 10)
-#define MAXPHDRS 4
+#define MAXPHDRS 5
#define MAXLIBDIRS 32
#define PATHLEN 256
#define MAXGOT (1 << 16)
@@ -118,6 +118,12 @@ struct outelf {
unsigned long tls_size;
unsigned long tls_filesz;
+ /* the notes of every object, laid out together after the code, which
+ * the PT_NOTE segment covers */
+ unsigned long note_faddr;
+ unsigned long note_vaddr;
+ unsigned long note_len;
+
Elf_Shdr shdr[MAXSECS];
int nsh;
char symstr[MAXSYMSTR];