|
|
Message-ID: <MWHPR22MB16792B19EA7487EA90BDB32CCB730@MWHPR22MB1679.namprd22.prod.outlook.com>
Date: Wed, 6 Mar 2019 13:14:17 +0000
From: Ray <i@...kray.me>
To: "musl@...ts.openwall.com" <musl@...ts.openwall.com>
Subject: [PATCH] resolve DT_RELR packed relative relocations
The SHT_RELR (DT_RELR) idea originated from the ChromeOS land but it
resulted in a proposal in the generic ABI mailing list
https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg (As I
understant it, that forum has no official maintainer now so the
dyanmic tag numbers cannot be officially
assigned).https://android-review.googlesource.com/c/platform/build/soong/+/709131/
has some saving numbers. 3.93% for some Android directory.
We may consider adopting this section type and benefit from its size
savings. In llvm, the lld linker can generate SHT_RELR (since
https://reviews.llvm.org/D48247) sections and llvm-readelf -r can
decode them (since https://reviews.llvm.org/D47919).
(I worry the webmail may break the tabs used in this patch. I hope it
wouldn't cause too much trouble)
From 2921fb00cb5967c1d55921f0c807980969caf90c Mon Sep 17 00:00:00 2001
From: Fangrui Song <i@...kray.me>
Date: Wed, 6 Mar 2019 10:06:14 +0000
Subject: [PATCH] resolve DT_RELR packed relative relocations
this doesn't resolve DT_RELR relocations in the dynamic linker itself.
proposal for adding SHT_RELR sections to generic-abi:
https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg
since llvm 7.0.0,
ld.lld --pack-dyn-relocs=relr can generate a SHT_RELR section.
llvm-readelf -r can decode SHT_RELR sections.
Signed-off-by: Fangrui Song <i@...kray.me>
---
include/elf.h | 8 ++++++--
ldso/dynlink.c | 17 +++++++++++++++++
src/internal/dynlink.h | 2 +-
3 files changed, 24 insertions(+), 3 deletions(-)
diff --git a/include/elf.h b/include/elf.h
index 54f41a10..41c27ac7 100644
--- a/include/elf.h
+++ b/include/elf.h
@@ -384,7 +384,8 @@ typedef struct {
#define SHT_PREINIT_ARRAY 16
#define SHT_GROUP 17
#define SHT_SYMTAB_SHNDX 18
-#define SHT_NUM 19
+#define SHT_RELR 19
+#define SHT_NUM 20
#define SHT_LOOS 0x60000000
#define SHT_GNU_ATTRIBUTES 0x6ffffff5
#define SHT_GNU_HASH 0x6ffffff6
@@ -744,7 +745,10 @@ typedef struct {
#define DT_PREINIT_ARRAY 32
#define DT_PREINIT_ARRAYSZ 33
#define DT_SYMTAB_SHNDX 34
-#define DT_NUM 35
+#define DT_RELRSZ 35
+#define DT_RELR 36
+#define DT_RELRENT 37
+#define DT_NUM 38
#define DT_LOOS 0x6000000d
#define DT_HIOS 0x6ffff000
#define DT_LOPROC 0x70000000
diff --git a/ldso/dynlink.c b/ldso/dynlink.c
index 35cacd76..bedae482 100644
--- a/ldso/dynlink.c
+++ b/ldso/dynlink.c
@@ -491,6 +491,22 @@ static void do_relocs(struct dso *dso, size_t
*rel, size_t rel_size, size_t stri
}
}
+static void do_relr_relocs(struct dso *dso, size_t *rel, size_t rel_size) {
+ unsigned char *base = dso->base;
+ size_t *rel_addr;
+ for (; rel_size; rel++, rel_size-=sizeof(size_t))
+ if (rel[0]%2 == 0) {
+ rel_addr = laddr(dso, rel[0]);
+ *rel_addr++ += (size_t)base;
+ } else {
+ int i = 0;
+ for (size_t bitmap=rel[0]; (bitmap>>=1); i++)
+ if (bitmap&1)
+ rel_addr[i] += (size_t)base;
+ rel_addr += 8*sizeof(size_t)-1;
+ }
+}
+
static void redo_lazy_relocs()
{
struct dso *p = lazy_head, *next;
@@ -1314,6 +1330,7 @@ static void reloc_all(struct dso *p)
2+(dyn[DT_PLTREL]==DT_RELA));
do_relocs(p, laddr(p, dyn[DT_REL]), dyn[DT_RELSZ], 2);
do_relocs(p, laddr(p, dyn[DT_RELA]), dyn[DT_RELASZ], 3);
+ do_relr_relocs(p, laddr(p, dyn[DT_RELR]), dyn[DT_RELRSZ]);
if (head != &ldso && p->relro_start != p->relro_end &&
mprotect(laddr(p, p->relro_start), p->relro_end-p->relro_start, PROT_READ)
diff --git a/src/internal/dynlink.h b/src/internal/dynlink.h
index cbe0a6fe..c55ca9e7 100644
--- a/src/internal/dynlink.h
+++ b/src/internal/dynlink.h
@@ -92,7 +92,7 @@ struct fdpic_dummy_loadmap {
#endif
#define AUX_CNT 32
-#define DYN_CNT 32
+#define DYN_CNT 37
typedef void (*stage2_func)(unsigned char *, size_t *);
typedef _Noreturn void (*stage3_func)(size_t *);
--
2.20.1
Powered by blists - more mailing lists
Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.