commit f10c64b2c0179267cfdefedc6b0cd4e15739a4c1
parent 8e3f308c38ca57e05cf628bd0040a5b875e6c4fb
Author: Roberto Vargas <roberto.vargas@arm.com>
Date: Fri, 26 Oct 2018 14:05:24 +0100
Add reloc()
This function is used to dynamically rellocate pointers
that are statically allocated.
Diffstat:
3 files changed, 16 insertions(+), 9 deletions(-)
diff --git a/include/rcode.h b/include/rcode.h
@@ -163,5 +163,5 @@ extern uint32_t outm32(uint32_t, void *addr);
/* global constant variables */
extern const struct rowidx rowidx[];
-extern const ptrdiff_t handler[];
+extern const void *const handler[];
extern const char *const ecstr[];
diff --git a/scripts/gentbl.sh b/scripts/gentbl.sh
@@ -36,7 +36,6 @@ sort -n -k1,2 < $in |
awk '
BEGIN {
print "#include <rcode.h>\n"
- print "#define from0(f) ((char *) (f) - (char *) 0)\n"
};
NF == 4 && $1 ~ /[0-9]*/ && $2 ~ /[0-9]*/ && /'$img'/ {
@@ -59,9 +58,9 @@ END {
}
print "};"
- print "const ptrdiff_t handler[] = {"
+ print "const void *const handler[] = {"
for (row in rowcnt)
for (col = 0; col < rowcnt[row]; col++)
- printf "\t[%d] = from0(hdl_%s),\n", begin[row] + col, cmds[row,col]
+ printf "\t[%d] = hdl_%s,\n", begin[row] + col, cmds[row,col]
print "};"
}' > $tmp && mv $tmp rmctbl.c
diff --git a/src/rmc.c b/src/rmc.c
@@ -74,6 +74,15 @@ panic(const char *msg)
dopanic();
}
+static inline void *
+reloc(const void *addr)
+{
+ uintptr_t a = (uintptr_t) addr;
+ uintptr_t off = (uintptr_t) bss->text;
+
+ return (void *) (a + off);
+}
+
void
trap(struct trapframe *fp)
{
@@ -95,7 +104,7 @@ trap(struct trapframe *fp)
msg = ecstr[ec];
if (!msg)
msg = "unknown reason";
- panicfmt(msg, fp);
+ panicfmt(reloc(msg), fp);
halt();
}
@@ -145,7 +154,7 @@ rmc(Rmucmd *cmd)
{
const struct rowidx *idx;
void (*fp)(Rmucmd *cmd);
- char *bp;
+ void *bp;
if (cmd->class > 255 || cmd->func > 255)
panic("rmc");
@@ -159,9 +168,8 @@ rmc(Rmucmd *cmd)
* because we are linked at address 0 but we can be
* loaded at any address
*/
- bp = bss->text;
- bp += handler[idx->off + cmd->func];
- fp = (void (*)(Rmucmd *)) bp;
+ bp = reloc(handler[idx->off + cmd->func]);
+ fp = (void (*) (Rmucmd *)) bp;
(*fp)(cmd);
cmd->fp->x0 = SUCCESS;