All of lore.kernel.org
 help / color / mirror / Atom feed
From: Michal Simek <monstr@monstr.eu>
To: u-boot@lists.denx.de, git@xilinx.com
Cc: Alistair Delva <adelva@google.com>
Subject: [PATCH v4 22/23] tools: relocate-rela: Add support for 32bit Microblaze relocation
Date: Wed, 15 Jun 2022 15:21:18 +0200	[thread overview]
Message-ID: <9912c3d76933bdf75e1ebb6aab43726cd32cafb5.1655299267.git.michal.simek@amd.com> (raw)
In-Reply-To: <cover.1655299267.git.michal.simek@amd.com>

Microblaze is 32bit that's why it is using elf32 format. Relocation code
requires to get information about rela and dynsym senctions and also text
base which was used for compilation.
Code build with -fPIC and linked with -pic generates 4 relocation types.
R_MICROBLAZE_NONE is the easiest one which doesn't require any action.
R_MICROBLAZE_REL only requires write addend to r_offset address.
R_MICROBLAZE_32/R_MICROBLAZE_GLOB_DAT are the most complicated. There is a
need to find out symbol value with adding symbol value and write it to
address pointed by r_offset. Calculation with addend is also added but
only 0 addend values are generated now.

Signed-off-by: Michal Simek <michal.simek@amd.com>
---

(no changes since v1)

 tools/relocate-rela.c | 166 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 166 insertions(+)

diff --git a/tools/relocate-rela.c b/tools/relocate-rela.c
index 7c2a441a8e91..090fb1acb20c 100644
--- a/tools/relocate-rela.c
+++ b/tools/relocate-rela.c
@@ -420,6 +420,170 @@ static int rela_elf64(char **argv, FILE *f)
 	return 0;
 }
 
+static bool supported_rela32(Elf32_Rela *rela, uint32_t *type)
+{
+	uint32_t mask = 0xffULL; /* would be different on 32-bit */
+	*type = rela->r_info & mask;
+
+	debug("Type:\t");
+
+	switch (*type) {
+	case R_MICROBLAZE_32:
+		debug("R_MICROBLAZE_32\n");
+		return true;
+	case R_MICROBLAZE_GLOB_DAT:
+		debug("R_MICROBLAZE_GLOB_DAT\n");
+		return true;
+	case R_MICROBLAZE_NONE:
+		debug("R_MICROBLAZE_NONE - ignoring - do nothing\n");
+		return false;
+	case R_MICROBLAZE_REL:
+		debug("R_MICROBLAZE_REL\n");
+		return true;
+	default:
+		fprintf(stderr, "warning: unsupported relocation type %"
+			PRIu32 " at %" PRIx32 "\n", *type, rela->r_offset);
+
+		return false;
+	}
+}
+
+static int rela_elf32(char **argv, FILE *f)
+{
+	int i, num, index;
+	uint32_t value, type;
+
+	if ((rela_end - rela_start) % sizeof(Elf32_Rela)) {
+		fprintf(stderr, "%s: rela size isn't a multiple of Elf32_Rela\n", argv[0]);
+		return 3;
+	}
+
+	num = (rela_end - rela_start) / sizeof(Elf32_Rela);
+
+	debug("Number of entries: %u\n", num);
+
+	for (i = 0; i < num; i++) {
+		Elf32_Rela rela, swrela;
+		Elf32_Sym symbols;
+		uint32_t pos = rela_start + sizeof(Elf32_Rela) * i;
+		uint32_t addr, pos_dyn;
+
+		debug("\nPossition:\t%d/0x%x\n", i, pos);
+
+		if (fseek(f, pos, SEEK_SET) < 0) {
+			fprintf(stderr, "%s: %s: seek to %" PRIx32
+					" failed: %s\n",
+				argv[0], argv[1], pos, strerror(errno));
+		}
+
+		if (fread(&rela, sizeof(rela), 1, f) != 1) {
+			fprintf(stderr, "%s: %s: read rela failed at %"
+					PRIx32 "\n",
+				argv[0], argv[1], pos);
+			return 4;
+		}
+
+		debug("Rela:\toffset:\t%" PRIx32 " r_info:\t%"
+		      PRIu32 " r_addend:\t%" PRIx32 "\n",
+		      rela.r_offset, rela.r_info, rela.r_addend);
+
+		swrela.r_offset = cpu_to_le32(rela.r_offset);
+		swrela.r_info = cpu_to_le32(rela.r_info);
+		swrela.r_addend = cpu_to_le32(rela.r_addend);
+
+		debug("SWRela:\toffset:\t%" PRIx32 " r_info:\t%"
+		      PRIu32 " r_addend:\t%" PRIx32 "\n",
+		      swrela.r_offset, swrela.r_info, swrela.r_addend);
+
+		if (!supported_rela32(&swrela, &type))
+			continue;
+
+		if (swrela.r_offset < text_base) {
+			fprintf(stderr, "%s: %s: bad rela at %" PRIx32 "\n",
+				argv[0], argv[1], pos);
+			return 4;
+		}
+
+		addr = swrela.r_offset - text_base;
+
+		debug("Addr:\t0x%" PRIx32 "\n", addr);
+
+		switch (type) {
+		case R_MICROBLAZE_REL:
+			if (fseek(f, addr, SEEK_SET) < 0) {
+				fprintf(stderr, "%s: %s: seek to %"
+					PRIx32 " failed: %s\n",
+					argv[0], argv[1], addr, strerror(errno));
+				return 5;
+			}
+
+			debug("Write addend\n");
+
+			if (fwrite(&rela.r_addend, sizeof(rela.r_addend), 1, f) != 1) {
+				fprintf(stderr, "%s: %s: write failed at %" PRIx32 "\n",
+					argv[0], argv[1], addr);
+				return 4;
+			}
+			break;
+		case R_MICROBLAZE_32:
+		case R_MICROBLAZE_GLOB_DAT:
+			/* global symbols read it and add reloc offset */
+			index = swrela.r_info >> 8;
+			pos_dyn = dyn_start + sizeof(Elf32_Sym) * index;
+
+			debug("Index:\t%d\n", index);
+			debug("Pos_dyn:\t0x%x\n", pos_dyn);
+
+			if (fseek(f, pos_dyn, SEEK_SET) < 0) {
+				fprintf(stderr, "%s: %s: seek to %"
+					PRIx32 " failed: %s\n",
+					argv[0], argv[1], pos_dyn, strerror(errno));
+				return 5;
+			}
+
+			if (fread(&symbols, sizeof(symbols), 1, f) != 1) {
+				fprintf(stderr, "%s: %s: read symbols failed at %"
+						PRIx32 "\n",
+					argv[0], argv[1], pos_dyn);
+				return 4;
+			}
+
+			debug("Symbol description:\n");
+			debug(" st_name:\t0x%x\n", symbols.st_name);
+			debug(" st_value:\t0x%x\n", symbols.st_value);
+			debug(" st_size:\t0x%x\n", symbols.st_size);
+
+			value = swrela.r_addend + symbols.st_value;
+
+			debug("Value:\t0x%x\n", value);
+
+			if (fseek(f, addr, SEEK_SET) < 0) {
+				fprintf(stderr, "%s: %s: seek to %"
+					PRIx32 " failed: %s\n",
+					argv[0], argv[1], addr, strerror(errno));
+				return 5;
+			}
+
+			if (fwrite(&value, sizeof(rela.r_addend), 1, f) != 1) {
+				fprintf(stderr, "%s: %s: write failed at %" PRIx32 "\n",
+					argv[0], argv[1], addr);
+				return 4;
+			}
+
+			break;
+		case R_MICROBLAZE_NONE:
+			debug("R_MICROBLAZE_NONE - skip\n");
+			break;
+		default:
+			fprintf(stderr, "warning: unsupported relocation type %"
+				PRIu32 " at %" PRIx32 "\n",
+				type, rela.r_offset);
+		}
+	}
+
+	return 0;
+}
+
 int main(int argc, char **argv)
 {
 	FILE *f;
@@ -467,6 +631,8 @@ int main(int argc, char **argv)
 
 	if (ei_class == 2)
 		ret = rela_elf64(argv, f);
+	else
+		ret = rela_elf32(argv, f);
 
 	if (fclose(f) < 0) {
 		fprintf(stderr, "%s: %s: close failed: %s\n",
-- 
2.36.1


  parent reply	other threads:[~2022-06-15 13:25 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-15 13:20 [PATCH v4 00/23] microblaze: Add support for full relocation Michal Simek
2022-06-15 13:20 ` [PATCH v4 01/23] tools: relocate-rela: Open binary u-boot file later Michal Simek
2022-06-15 13:20 ` [PATCH v4 02/23] Makefile: Fix description for relocate-rela parameters Michal Simek
2022-06-15 13:20 ` [PATCH v4 03/23] tools: relocate-rela: Use global variables Michal Simek
2022-06-15 13:21 ` [PATCH v4 04/23] tools: relocate-rela: Read rela start/end directly from ELF Michal Simek
2022-06-15 13:21 ` [PATCH v4 05/23] microblaze: Switch absolute branches to relative Michal Simek
2022-06-15 13:21 ` [PATCH v4 06/23] microblaze: Fix stack protection behavior Michal Simek
2022-06-15 13:21 ` [PATCH v4 07/23] microblaze: Fix early stack allocation Michal Simek
2022-06-15 13:21 ` [PATCH v4 08/23] microblaze: Remove CONFIG_TEXT_BASE from code Michal Simek
2022-06-15 13:21 ` [PATCH v4 09/23] microblaze: Fix typo in exception.c Michal Simek
2022-06-15 13:21 ` [PATCH v4 10/23] mips: Move endianness selection to arch/Kconfig Michal Simek
2022-06-15 13:21 ` [PATCH v4 11/23] microblaze: Enable REMAKE_ELF Michal Simek
2022-06-15 13:21 ` [PATCH v4 12/23] microblaze: Separate code end substraction Michal Simek
2022-06-15 13:21 ` [PATCH v4 13/23] microblaze: Change stack protection address to new stack address Michal Simek
2022-06-15 13:21 ` [PATCH v4 14/23] microblaze: Optimize register usage in relocate_code Michal Simek
2022-06-15 13:21 ` [PATCH v4 15/23] microblaze: Remove code around r20 in relocate_code() Michal Simek
2022-06-15 13:21 ` [PATCH v4 16/23] microblaze: Remove _start symbol handling at U-Boot start Michal Simek
2022-06-15 13:21 ` [PATCH v4 17/23] microblaze: Add comment about reset location Michal Simek
2022-06-15 13:21 ` [PATCH v4 18/23] microblaze: Create SYM_ADDR macro to deal with symbols Michal Simek
2022-06-15 13:21 ` [PATCH v4 19/23] tools: relocate-rela: Extract elf64 reloc to special function Michal Simek
2022-06-15 13:21 ` [PATCH v4 20/23] tools: relocate-rela: Check that relocation works only for EM_AARCH64 Michal Simek
2022-06-15 13:21 ` [PATCH v4 21/23] tools: relocate-rela: Add support for elf32 decoding Michal Simek
2022-06-15 13:21 ` Michal Simek [this message]
2022-06-15 13:21 ` [PATCH v4 23/23] microblaze: Add support for run time relocation Michal Simek
2022-06-24 12:15 ` [PATCH v4 00/23] microblaze: Add support for full relocation Michal Simek

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=9912c3d76933bdf75e1ebb6aab43726cd32cafb5.1655299267.git.michal.simek@amd.com \
    --to=monstr@monstr.eu \
    --cc=adelva@google.com \
    --cc=git@xilinx.com \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.