Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 5 Feb 2018 23:35:34 +0000 (UTC)
From:      John Baldwin <jhb@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r328911 - in head: stand/common sys/kern
Message-ID:  <201802052335.w15NZYpI053647@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: jhb
Date: Mon Feb  5 23:35:33 2018
New Revision: 328911
URL: https://svnweb.freebsd.org/changeset/base/328911

Log:
  Ignore relocation tables for non-memory-resident sections.
  
  As a followup to r328101, ignore relocation tables for ELF object
  sections that are not memory resident.  For modules loaded by the
  loader, ignore relocation tables whose associated section was not
  loaded by the loader (sh_addr is zero).  For modules loaded at runtime
  via kldload(2), ignore relocation tables whose associated section is
  not marked with SHF_ALLOC.
  
  Reported by:	Mori Hiroki <yamori813@yahoo.co.jp>, adrian
  Tested on:	mips, mips64
  MFC after:	1 month
  Sponsored by:	DARPA / AFRL

Modified:
  head/stand/common/load_elf_obj.c
  head/sys/kern/link_elf_obj.c

Modified: head/stand/common/load_elf_obj.c
==============================================================================
--- head/stand/common/load_elf_obj.c	Mon Feb  5 23:29:50 2018	(r328910)
+++ head/stand/common/load_elf_obj.c	Mon Feb  5 23:35:33 2018	(r328911)
@@ -282,6 +282,8 @@ __elfN(obj_loadimage)(struct preloaded_file *fp, elf_f
 		switch (shdr[i].sh_type) {
 		case SHT_REL:
 		case SHT_RELA:
+			if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
+				break;
 			lastaddr = roundup(lastaddr, shdr[i].sh_addralign);
 			shdr[i].sh_addr = (Elf_Addr)lastaddr;
 			lastaddr += shdr[i].sh_size;

Modified: head/sys/kern/link_elf_obj.c
==============================================================================
--- head/sys/kern/link_elf_obj.c	Mon Feb  5 23:29:50 2018	(r328910)
+++ head/sys/kern/link_elf_obj.c	Mon Feb  5 23:35:33 2018	(r328911)
@@ -272,9 +272,17 @@ link_elf_link_preload(linker_class_t cls, const char *
 			symstrindex = shdr[i].sh_link;
 			break;
 		case SHT_REL:
+			/*
+			 * Ignore relocation tables for sections not
+			 * loaded by the loader.
+			 */
+			if (shdr[shdr[i].sh_info].sh_addr == 0)
+				break;
 			ef->nreltab++;
 			break;
 		case SHT_RELA:
+			if (shdr[shdr[i].sh_info].sh_addr == 0)
+				break;
 			ef->nrelatab++;
 			break;
 		}
@@ -398,12 +406,16 @@ link_elf_link_preload(linker_class_t cls, const char *
 			pb++;
 			break;
 		case SHT_REL:
+			if (shdr[shdr[i].sh_info].sh_addr == 0)
+				break;
 			ef->reltab[rl].rel = (Elf_Rel *)shdr[i].sh_addr;
 			ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel);
 			ef->reltab[rl].sec = shdr[i].sh_info;
 			rl++;
 			break;
 		case SHT_RELA:
+			if (shdr[shdr[i].sh_info].sh_addr == 0)
+				break;
 			ef->relatab[ra].rela = (Elf_Rela *)shdr[i].sh_addr;
 			ef->relatab[ra].nrela =
 			    shdr[i].sh_size / sizeof(Elf_Rela);
@@ -620,9 +632,17 @@ link_elf_load_file(linker_class_t cls, const char *fil
 			symstrindex = shdr[i].sh_link;
 			break;
 		case SHT_REL:
+			/*
+			 * Ignore relocation tables for unallocated
+			 * sections.
+			 */
+			if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
+				break;
 			ef->nreltab++;
 			break;
 		case SHT_RELA:
+			if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
+				break;
 			ef->nrelatab++;
 			break;
 		case SHT_STRTAB:
@@ -880,6 +900,8 @@ link_elf_load_file(linker_class_t cls, const char *fil
 			pb++;
 			break;
 		case SHT_REL:
+			if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
+				break;
 			ef->reltab[rl].rel = malloc(shdr[i].sh_size, M_LINKER,
 			    M_WAITOK);
 			ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel);
@@ -898,6 +920,8 @@ link_elf_load_file(linker_class_t cls, const char *fil
 			rl++;
 			break;
 		case SHT_RELA:
+			if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
+				break;
 			ef->relatab[ra].rela = malloc(shdr[i].sh_size, M_LINKER,
 			    M_WAITOK);
 			ef->relatab[ra].nrela =



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201802052335.w15NZYpI053647>