@@ -160,7 +160,7 @@ class ElfFile
160160
161161 Elf_Shdr & findSection (const SectionName & sectionName);
162162
163- Elf_Shdr * findSection2 (const SectionName & sectionName);
163+ std::optional<std::reference_wrapper< Elf_Shdr>> findSection2 (const SectionName & sectionName);
164164
165165 unsigned int findSection3 (const SectionName & sectionName);
166166
@@ -600,7 +600,7 @@ void ElfFile<ElfFileParamNames>::shiftFile(unsigned int extraPages, Elf_Addr sta
600600 PT_INTERP segment into memory. Otherwise glibc will choke. */
601601 phdrs.resize (rdi (hdr ()->e_phnum ) + 1 );
602602 wri (hdr ()->e_phnum , rdi (hdr ()->e_phnum ) + 1 );
603- Elf_Phdr & phdr = phdrs[ rdi (hdr ()->e_phnum ) - 1 ] ;
603+ Elf_Phdr & phdr = phdrs. at ( rdi (hdr ()->e_phnum ) - 1 ) ;
604604 wri (phdr.p_type , PT_LOAD);
605605 wri (phdr.p_offset , 0 );
606606 wri (phdr.p_vaddr , wri (phdr.p_paddr , startPage));
@@ -637,10 +637,12 @@ Elf_Shdr & ElfFile<ElfFileParamNames>::findSection(const SectionName & sectionNa
637637
638638
639639template <ElfFileParams>
640- Elf_Shdr * ElfFile<ElfFileParamNames>::findSection2(const SectionName & sectionName)
640+ std::optional<std::reference_wrapper< Elf_Shdr>> ElfFile<ElfFileParamNames>::findSection2(const SectionName & sectionName)
641641{
642642 auto i = findSection3 (sectionName);
643- return i ? &shdrs[i] : nullptr ;
643+ if (i)
644+ return shdrs.at (i);
645+ return {};
644646}
645647
646648
@@ -861,7 +863,7 @@ void ElfFile<ElfFileParamNames>::rewriteSectionsLibrary()
861863 wri (hdr ()->e_phoff , sizeof (Elf_Ehdr));
862864 phdrs.resize (rdi (hdr ()->e_phnum ) + 1 );
863865 wri (hdr ()->e_phnum , rdi (hdr ()->e_phnum ) + 1 );
864- Elf_Phdr & phdr = phdrs[ rdi (hdr ()->e_phnum ) - 1 ] ;
866+ Elf_Phdr & phdr = phdrs. at ( rdi (hdr ()->e_phnum ) - 1 ) ;
865867 wri (phdr.p_type , PT_LOAD);
866868 wri (phdr.p_offset , startOffset);
867869 wri (phdr.p_vaddr , wri (phdr.p_paddr , startPage));
@@ -914,7 +916,7 @@ void ElfFile<ElfFileParamNames>::rewriteSectionsExecutable()
914916 Elf_Addr startAddr = rdi (shdrs.at (lastReplaced + 1 ).sh_addr );
915917 std::string prevSection;
916918 for (unsigned int i = 1 ; i <= lastReplaced; ++i) {
917- Elf_Shdr & shdr (shdrs[i] );
919+ Elf_Shdr & shdr (shdrs. at (i) );
918920 std::string sectionName = getSectionName (shdr);
919921 debug (" looking at section '%s'\n " , sectionName.c_str ());
920922 /* !!! Why do we stop after a .dynstr section? I can't
@@ -955,7 +957,7 @@ void ElfFile<ElfFileParamNames>::rewriteSectionsExecutable()
955957 assert (rdi (hdr ()->e_shnum ) == shdrs.size ());
956958 sortShdrs ();
957959 for (unsigned int i = 1 ; i < rdi (hdr ()->e_shnum ); ++i)
958- * ((Elf_Shdr *) (fileContents->data () + rdi (hdr ()->e_shoff )) + i) = shdrs[i] ;
960+ * ((Elf_Shdr *) (fileContents->data () + rdi (hdr ()->e_shoff )) + i) = shdrs. at (i) ;
959961 }
960962
961963
@@ -1121,7 +1123,7 @@ void ElfFile<ElfFileParamNames>::rewriteHeaders(Elf_Addr phdrAddress)
11211123 (e.g., those produced by klibc's klcc). */
11221124 auto shdrDynamic = findSection2 (" .dynamic" );
11231125 if (shdrDynamic) {
1124- auto dyn_table = (Elf_Dyn *) (fileContents->data () + rdi (shdrDynamic-> sh_offset ));
1126+ auto dyn_table = (Elf_Dyn *) (fileContents->data () + rdi ((* shdrDynamic). get (). sh_offset ));
11251127 unsigned int d_tag;
11261128 for (auto dyn = dyn_table; (d_tag = rdi (dyn->d_tag )) != DT_NULL; dyn++)
11271129 if (d_tag == DT_STRTAB)
@@ -1136,13 +1138,14 @@ void ElfFile<ElfFileParamNames>::rewriteHeaders(Elf_Addr phdrAddress)
11361138 auto shdr = findSection2 (" .gnu.hash" );
11371139 // some binaries might this section stripped
11381140 // in which case we just ignore the value.
1139- if (shdr) dyn->d_un .d_ptr = shdr-> sh_addr ;
1141+ if (shdr) dyn->d_un .d_ptr = (* shdr). get (). sh_addr ;
11401142 } else if (d_tag == DT_JMPREL) {
11411143 auto shdr = findSection2 (" .rel.plt" );
1142- if (!shdr) shdr = findSection2 (" .rela.plt" ); /* 64-bit Linux, x86-64 */
1144+ if (!shdr) shdr = findSection2 (" .rela.plt" );
1145+ /* 64-bit Linux, x86-64 */
11431146 if (!shdr) shdr = findSection2 (" .rela.IA_64.pltoff" ); /* 64-bit Linux, IA-64 */
11441147 if (!shdr) error (" cannot find section corresponding to DT_JMPREL" );
1145- dyn->d_un .d_ptr = shdr-> sh_addr ;
1148+ dyn->d_un .d_ptr = (* shdr). get (). sh_addr ;
11461149 }
11471150 else if (d_tag == DT_REL) { /* !!! hack! */
11481151 auto shdr = findSection2 (" .rel.dyn" );
@@ -1152,14 +1155,14 @@ void ElfFile<ElfFileParamNames>::rewriteHeaders(Elf_Addr phdrAddress)
11521155 /* some programs have neither section, but this doesn't seem
11531156 to be a problem */
11541157 if (!shdr) continue ;
1155- dyn->d_un .d_ptr = shdr-> sh_addr ;
1158+ dyn->d_un .d_ptr = (* shdr). get (). sh_addr ;
11561159 }
11571160 else if (d_tag == DT_RELA) {
11581161 auto shdr = findSection2 (" .rela.dyn" );
11591162 /* some programs lack this section, but it doesn't seem to
11601163 be a problem */
11611164 if (!shdr) continue ;
1162- dyn->d_un .d_ptr = shdr-> sh_addr ;
1165+ dyn->d_un .d_ptr = (* shdr). get (). sh_addr ;
11631166 }
11641167 else if (d_tag == DT_VERNEED)
11651168 dyn->d_un .d_ptr = findSection (" .gnu.version_r" ).sh_addr ;
@@ -1172,7 +1175,7 @@ void ElfFile<ElfFileParamNames>::rewriteHeaders(Elf_Addr phdrAddress)
11721175 if (shdr) {
11731176 auto rld_map_addr = findSection (" .rld_map" ).sh_addr ;
11741177 auto dyn_offset = ((char *)dyn) - ((char *)dyn_table);
1175- dyn->d_un .d_ptr = rld_map_addr + dyn_offset - shdrDynamic-> sh_addr ;
1178+ dyn->d_un .d_ptr = rld_map_addr + dyn_offset - (* shdrDynamic). get (). sh_addr ;
11761179 } else {
11771180 /* ELF file with DT_MIPS_RLD_MAP_REL but without .rld_map
11781181 is broken, and it's not our job to fix it; yet, we have
@@ -1346,12 +1349,12 @@ std::string ElfFile<ElfFileParamNames>::shrinkRPath(char* rpath, std::vector<std
13461349 exists in this directory. */
13471350 bool libFound = false ;
13481351 for (unsigned int j = 0 ; j < neededLibs.size (); ++j)
1349- if (!neededLibFound[j] ) {
1350- std::string libName = dirName + " /" + neededLibs[j] ;
1352+ if (!neededLibFound. at (j) ) {
1353+ std::string libName = dirName + " /" + neededLibs. at (j) ;
13511354 try {
13521355 Elf32_Half library_e_machine = getElfType (readFile (libName, sizeof (Elf32_Ehdr))).machine ;
13531356 if (rdi (library_e_machine) == rdi (hdr ()->e_machine )) {
1354- neededLibFound[j] = true ;
1357+ neededLibFound. at (j) = true ;
13551358 libFound = true ;
13561359 } else
13571360 debug (" ignoring library '%s' because its machine type differs\n " , libName.c_str ());
0 commit comments