Skip to content

Commit 772dc70

Browse files
committed
Add PT_GNU_STACK segment if supported
Recent glibc reject dlopen on shared libs containing no or a PF_X PT_GNU_STACK header, so lets emit something reasonable: When any of the input files (among them the systems crt*.o) have a .note.GNU-stack section we assume that that program header type is supported by the systems loader. We then emit a PT_GNU_STACK program header (always at least read-write no matter the input sections flags) and add PF_X if any input file did in fact request an executable stack. We don't support configuring that from command line.
1 parent 49ed952 commit 772dc70

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

‎tcc.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,7 @@ struct TCCState {
935935
#define qrel s1->qrel
936936

937937
addr_t tls_start, tls_end;
938+
int seen_execstack;
938939

939940
#ifdef TCC_TARGET_RISCV64
940941
struct pcrel_hi { addr_t addr, val; } **pcrel_hi_entries;

‎tccelf.c‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2417,7 +2417,7 @@ static int layout_sections(TCCState *s1, int *sec_order, struct dyn_inf *d)
24172417
Section *s;
24182418
addr_t addr, tmp, align, s_align, base;
24192419
ElfW(Phdr) *ph = NULL, *ph2;
2420-
int i, f, n, phnum, phfill;
2420+
int i, f, n, phnum, phfill, execstack;
24212421
int file_offset;
24222422

24232423
/* compute number of program headers */
@@ -2436,6 +2436,8 @@ static int layout_sections(TCCState *s1, int *sec_order, struct dyn_inf *d)
24362436
d->ehfr = phnum++;
24372437
if (d->relro)
24382438
d->relro = phnum++;
2439+
if (s1->seen_execstack)
2440+
execstack = phnum++;
24392441
d->phnum = phnum;
24402442
d->phdr = tcc_mallocz(phnum * sizeof(ElfW(Phdr)));
24412443

@@ -2552,6 +2554,12 @@ static int layout_sections(TCCState *s1, int *sec_order, struct dyn_inf *d)
25522554
fill_phdr(&d->phdr[d->ehfr], PT_GNU_EH_FRAME, eh_frame_hdr_section);
25532555
if (d->interp)
25542556
fill_phdr(&d->phdr[1], PT_INTERP, d->interp);
2557+
if (execstack) {
2558+
fill_phdr(&d->phdr[execstack], PT_GNU_STACK, NULL);
2559+
d->phdr[execstack].p_flags =
2560+
PF_R | PF_W | (s1->seen_execstack & SHF_EXECINSTR ? PF_X : 0);
2561+
d->phdr[execstack].p_align = 16;
2562+
}
25552563
if (phfill) {
25562564
ph = &d->phdr[0];
25572565
ph->p_offset = sizeof(ElfW(Ehdr));
@@ -3447,6 +3455,9 @@ ST_FUNC int tcc_load_object_file(TCCState *s1,
34473455
ptr = s->data + offset;
34483456
full_read(fd, ptr, size);
34493457
}
3458+
if (!strcmp(sh_name, ".note.GNU-stack"))
3459+
s1->seen_execstack |=
3460+
SHF_ALLOC | (s->sh_flags & (SHF_WRITE|SHF_EXECINSTR));
34503461
#if defined TCC_TARGET_ARM || defined TCC_TARGET_ARM64 || defined TCC_TARGET_RISCV64
34513462
/* align code sections to instruction lenght */
34523463
/* This is needed if we compile a c file after this */

0 commit comments

Comments
 (0)