public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: binutils@emagii.com
To: binutils@sourceware.org
Cc: nickc@redhat.com, Ulf Samuelsson <binutils@emagii.com>
Subject: [PATCH v0 6/6] ldlang.c: Try to get the .text section for checking CRC
Date: Thu, 16 Feb 2023 21:40:06 +0100	[thread overview]
Message-ID: <20230216204006.1977-7-binutils@emagii.com> (raw)
In-Reply-To: <20230216204006.1977-1-binutils@emagii.com>

From: Ulf Samuelsson <binutils@emagii.com>

This fails to get the section:
  if ( bfd_get_section_contents (link_info.output_bfd,
	  ts,
	  text_section,
	  0,
	  ts->size))

Signed-off-by: Ulf Samuelsson <binutils@emagii.com>
---
 ld/ldlang.c | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 108 insertions(+)

diff --git a/ld/ldlang.c b/ld/ldlang.c
index e0bb079b34e..5d88e3aa9e5 100644
--- a/ld/ldlang.c
+++ b/ld/ldlang.c
@@ -6847,6 +6847,26 @@ lang_symbol_tweaks (void)
     }
 }
 
+static bool symbol_lookup(char *name, bfd_vma *val)
+{
+  struct bfd_link_hash_entry *h;
+  h = bfd_link_hash_lookup (link_info.hash, name,
+			    false, false, true);
+  if (h != NULL)
+    {
+      if ((h->type == bfd_link_hash_defined
+	  || h->type == bfd_link_hash_defweak)
+	 && h->u.def.section->output_section != NULL)
+	{
+	  *val = (h->u.def.value
+	     + bfd_section_vma (h->u.def.section->output_section)
+	     + h->u.def.section->output_offset);
+	  return true;
+	}
+    }
+    return false;
+}
+
 static void
 lang_end (void)
 {
@@ -6953,6 +6973,94 @@ lang_end (void)
 		   entry_symbol.name);
 	}
     }
+
+
+  /* Check if we can calculate CRC64 */
+  /* All of CRC_ADDRESS CRC_START CRC_END must be defined */
+  {
+    bfd_vma crc64, start, end;
+    bool can_do_crc;
+
+    can_do_crc = symbol_lookup(CRC_ADDRESS, &crc64) &&
+		 symbol_lookup(CRC_START, &start)   &&
+		 symbol_lookup(CRC_END, &end);
+    if (can_do_crc)
+      {
+	asection *ts;
+
+	ts = bfd_get_section_by_name (link_info.output_bfd, entry_section);
+	if (ts != NULL) {
+	  bfd_vma text_start = ts->lma;
+	  bfd_vma text_end   = ts->lma + ts->size;
+	  printf("%s: [0x%08lx .. 0x%08lx]\n",
+		ts->name,
+		text_start,
+		text_end);
+	  bool OK =
+	    ((text_start <= crc64) && (crc64 <= text_end)) &&
+	    ((text_start <= start) && (start <= text_end)) &&
+	    ((text_start <= end)   && (end   <= text_end));
+	  if (OK)
+	    {
+	      printf("All the memory to be CRC checked is in the text section\n");
+	       /* allocate twice the size, since we do not know what we are doing */
+	      bfd_vma *text_section = malloc(ts->size * 2);
+	      if (text_section != NULL)
+		{
+		  if ( bfd_get_section_contents (link_info.output_bfd,
+			  ts,
+			  text_section,
+			  0,
+			  ts->size))
+		    {
+		      for (bfd_vma i = 0 ; i < ts->size ; i+=4)
+			{
+			  printf("0x%08lx: 0x%08lx 0x%08lx 0x%08lx 0x%08lx\n",
+				ts->lma + i,
+				text_section[i+0],
+				text_section[i+1],
+				text_section[i+2],
+				text_section[i+3]);
+			}
+		    }
+		  else
+		    {
+		      printf("Could not fetch contents of %s\n", ts->name);
+		    }
+		  free(text_section);
+		}
+	    }
+	}
+	printf("%-20s = 0x%08lx\n",CRC_ADDRESS , crc64);
+	ts = bfd_get_linker_section (link_info.output_bfd, CRC_ADDRESS);
+	if (ts != NULL) {
+	  printf("%s: [0x%08lx .. 0x%08lx]\n",
+		ts->name,
+		ts->lma,
+		ts->lma + ts->size);
+	}
+	printf("%-20s = 0x%08lx\n",CRC_START , start);
+	ts = bfd_get_linker_section (link_info.output_bfd, CRC_START);
+	if (ts != NULL) {
+	  printf("%s: [0x%08lx .. 0x%08lx]\n",
+		ts->name,
+		ts->lma,
+		ts->lma + ts->size);
+	}
+	printf("%-20s = 0x%08lx\n",CRC_END , end);
+	ts = bfd_get_linker_section (link_info.output_bfd, CRC_END);
+	if (ts != NULL) {
+	  printf("%s: [0x%08lx .. 0x%08lx]\n",
+		ts->name,
+		ts->lma,
+		ts->lma + ts->size);
+	}
+      }
+    else
+      {
+	printf("%-20s not found\n", CRC_ADDRESS);
+      }
+  }
 }
 
 /* This is a small function used when we want to ignore errors from
-- 
2.17.1


  parent reply	other threads:[~2023-02-16 20:40 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-16 20:40 [RFC v0 0/1] Add support for CRC64 generation in linker binutils
2023-02-16 20:40 ` [PATCH v0 1/6] CRC64 header binutils
2023-02-16 20:40 ` [PATCH v0 2/6] ldlang.h: CRC64 binutils
2023-02-16 20:40 ` [PATCH v0 3/6] ldlex.l: CRC64 binutils
2023-02-16 20:40 ` [PATCH v0 4/6] ldgram.y: CRC64 binutils
2023-02-16 20:40 ` [PATCH v0 5/6] ldlang.c: CRC64 binutils
2023-02-16 20:40 ` binutils [this message]
2023-02-16 21:30 ` [RFC v0 0/1] Add support for CRC64 generation in linker Fangrui Song
     [not found] ` <DS7PR12MB57657A0E46493FAAA203AF77CBA09@DS7PR12MB5765.namprd12.prod.outlook.com>
2023-02-16 22:37   ` Ulf Samuelsson
2023-02-17 11:11     ` Nick Clifton
2023-02-17 12:03       ` Ulf Samuelsson
2023-03-06  7:50         ` Fangrui Song
     [not found]         ` <DS7PR12MB57654E11983392D5DCBF1D85CBB69@DS7PR12MB5765.namprd12.prod.outlook.com>
2023-03-06 10:00           ` Ulf Samuelsson
2023-02-17  7:53 ` Ulf Samuelsson
2023-02-17 10:55   ` Nick Clifton
     [not found] ` <DS7PR12MB5765096101240054A648F8C9CBA09@DS7PR12MB5765.namprd12.prod.outlook.com>
2023-02-17 10:46   ` Nick Clifton

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=20230216204006.1977-7-binutils@emagii.com \
    --to=binutils@emagii.com \
    --cc=binutils@sourceware.org \
    --cc=nickc@redhat.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).