1*5ac3bc71Schristos.\" $NetBSD: elf_update.3,v 1.6 2024/03/03 17:37:33 christos Exp $ 2e81373b4Schristos.\" 39dd9d0cfSchristos.\" Copyright (c) 2006-2011 Joseph Koshy. All rights reserved. 49dd9d0cfSchristos.\" 59dd9d0cfSchristos.\" Redistribution and use in source and binary forms, with or without 69dd9d0cfSchristos.\" modification, are permitted provided that the following conditions 79dd9d0cfSchristos.\" are met: 89dd9d0cfSchristos.\" 1. Redistributions of source code must retain the above copyright 99dd9d0cfSchristos.\" notice, this list of conditions and the following disclaimer. 109dd9d0cfSchristos.\" 2. Redistributions in binary form must reproduce the above copyright 119dd9d0cfSchristos.\" notice, this list of conditions and the following disclaimer in the 129dd9d0cfSchristos.\" documentation and/or other materials provided with the distribution. 139dd9d0cfSchristos.\" 149dd9d0cfSchristos.\" This software is provided by Joseph Koshy ``as is'' and 159dd9d0cfSchristos.\" any express or implied warranties, including, but not limited to, the 169dd9d0cfSchristos.\" implied warranties of merchantability and fitness for a particular purpose 179dd9d0cfSchristos.\" are disclaimed. in no event shall Joseph Koshy be liable 189dd9d0cfSchristos.\" for any direct, indirect, incidental, special, exemplary, or consequential 199dd9d0cfSchristos.\" damages (including, but not limited to, procurement of substitute goods 209dd9d0cfSchristos.\" or services; loss of use, data, or profits; or business interruption) 219dd9d0cfSchristos.\" however caused and on any theory of liability, whether in contract, strict 229dd9d0cfSchristos.\" liability, or tort (including negligence or otherwise) arising in any way 239dd9d0cfSchristos.\" out of the use of this software, even if advised of the possibility of 249dd9d0cfSchristos.\" such damage. 259dd9d0cfSchristos.\" 263f622274Sjkoshy.\" Id: elf_update.3 3957 2022-03-12 14:11:52Z jkoshy 279dd9d0cfSchristos.\" 28c5261b83Sjkoshy.Dd April 22, 2019 299dd9d0cfSchristos.Dt ELF_UPDATE 3 30c5261b83Sjkoshy.Os 319dd9d0cfSchristos.Sh NAME 329dd9d0cfSchristos.Nm elf_update 339dd9d0cfSchristos.Nd update an ELF descriptor 349dd9d0cfSchristos.Sh LIBRARY 359dd9d0cfSchristos.Lb libelf 369dd9d0cfSchristos.Sh SYNOPSIS 379dd9d0cfSchristos.In libelf.h 389dd9d0cfSchristos.Ft off_t 399dd9d0cfSchristos.Fn elf_update "Elf *elf" "Elf_Cmd cmd" 409dd9d0cfSchristos.Sh DESCRIPTION 419dd9d0cfSchristosFunction 429dd9d0cfSchristos.Fn elf_update 439dd9d0cfSchristoscauses the library to recalculate the structure of an ELF 449dd9d0cfSchristosobject and optionally write out the image of the object 459dd9d0cfSchristosto file. 469dd9d0cfSchristos.Pp 479dd9d0cfSchristosArgument 483f622274Sjkoshy.Fa elf 499dd9d0cfSchristosshould reference a valid ELF descriptor. 509dd9d0cfSchristos.Pp 519dd9d0cfSchristosArgument 523f622274Sjkoshy.Fa cmd 539dd9d0cfSchristoscan be one of the following values: 549dd9d0cfSchristos.Bl -tag -width "Dv ELF_C_WRITE" 559dd9d0cfSchristos.It Dv ELF_C_NULL 569dd9d0cfSchristosThe library will recalculate structural information flagging 579dd9d0cfSchristosmodified structures with the 589dd9d0cfSchristos.Dv ELF_F_DIRTY 599dd9d0cfSchristosflag, but will not write data to the underlying file image. 609dd9d0cfSchristos.It Dv ELF_C_WRITE 619dd9d0cfSchristosThe library will recalculate structural information and will 629dd9d0cfSchristosalso write the new image to the underlying file. 639dd9d0cfSchristosThe ELF descriptor referenced by argument 643f622274Sjkoshy.Fa elf 659dd9d0cfSchristosshould permit the underlying ELF object to be written or updated 669dd9d0cfSchristos(see 679dd9d0cfSchristos.Xr elf_begin 3 ) . 689dd9d0cfSchristos.El 699dd9d0cfSchristos.Pp 709dd9d0cfSchristosAll pointers to 719dd9d0cfSchristos.Vt Elf_Scn 729dd9d0cfSchristosand 739dd9d0cfSchristos.Vt Elf_Data 749dd9d0cfSchristosdescriptors associated with descriptor 753f622274Sjkoshy.Fa elf 769dd9d0cfSchristosshould be considered invalid after a call to 779dd9d0cfSchristos.Fn elf_update . 789dd9d0cfSchristos.Ss Specifying Object Layout 799dd9d0cfSchristosThe 809dd9d0cfSchristos.Lb libelf 819dd9d0cfSchristossupports two layout modes. 829dd9d0cfSchristos.Bl -tag -width indent 839dd9d0cfSchristos.It "Library Layout" 849dd9d0cfSchristosIf the 859dd9d0cfSchristos.Dv ELF_F_LAYOUT 869dd9d0cfSchristosflag is not set on the ELF descriptor, the ELF library will lay out 879dd9d0cfSchristosthe ELF object according to the following scheme: 889dd9d0cfSchristos.Bl -tag -compact -width "Section Data" 899dd9d0cfSchristos.It Em EHDR 909dd9d0cfSchristosThe ELF executable header will be placed at the start of the object. 919dd9d0cfSchristos.It Em PHDR 929dd9d0cfSchristosIf the ELF descriptor contains a program header table, it will be 939dd9d0cfSchristosplaced after the Executable Header. 949dd9d0cfSchristos.It Em Section Data 959dd9d0cfSchristosELF section data, if any, will be placed next, keeping each section's 969dd9d0cfSchristosalignment requirements in mind. 979dd9d0cfSchristos.It Em SHDR 989dd9d0cfSchristosThe ELF section header table, if any, will be placed last. 999dd9d0cfSchristos.El 1009dd9d0cfSchristos.It "Application Controlled Layout" 1019dd9d0cfSchristosThe application can take full control of the layout of the ELF object 1029dd9d0cfSchristosby setting the 1039dd9d0cfSchristos.Dv ELF_F_LAYOUT 1049dd9d0cfSchristosflag on the ELF descriptor (see 1059dd9d0cfSchristos.Xr elf_flagelf 3 ) . 1069dd9d0cfSchristosIn this case the library will lay out the ELF object using 1079dd9d0cfSchristosapplication-supplied information as below: 1089dd9d0cfSchristos.Pp 1099dd9d0cfSchristos.Bl -tag -compact -width "Section Data" 1109dd9d0cfSchristos.It Em EHDR 1119dd9d0cfSchristosThe ELF executable header will be placed at the start of the object. 1129dd9d0cfSchristos.It Em PHDR 1139dd9d0cfSchristosThe ELF program header table, if any, it will be placed at the offset 1149dd9d0cfSchristosspecified in the 1159dd9d0cfSchristos.Va e_phoff 1169dd9d0cfSchristosfield of the ELF executable header. 1179dd9d0cfSchristos.It Em Section Data 1189dd9d0cfSchristosThe data for each ELF section will be placed at the offset specified 1199dd9d0cfSchristosby the 1209dd9d0cfSchristos.Va sh_offset 1219dd9d0cfSchristosfield of the section's header. 1229dd9d0cfSchristosThe size of the section will be taken from the 1239dd9d0cfSchristos.Va sh_size 1249dd9d0cfSchristosfield of the section header. 1259dd9d0cfSchristos.It Em SHDR 1269dd9d0cfSchristosThe ELF section header table, if any, will be placed at the offset 1279dd9d0cfSchristosspecified by the 1289dd9d0cfSchristos.Va e_shoff 1299dd9d0cfSchristosfield of the executable header. 1309dd9d0cfSchristos.El 1319dd9d0cfSchristos.El 1329dd9d0cfSchristos.Pp 1339dd9d0cfSchristosGaps in the coverage of the file's contents will be set to the fill value 1349dd9d0cfSchristosspecified by 1359dd9d0cfSchristos.Xr elf_fill 3 . 1369dd9d0cfSchristos.Ss Application Supplied Information 1379dd9d0cfSchristosThe application needs to set the following fields in the data 1389dd9d0cfSchristosstructures associated with the ELF descriptor prior to calling 1399dd9d0cfSchristos.Fn elf_update . 1409dd9d0cfSchristos.Bl -tag -width indent 1419dd9d0cfSchristos.It "Executable Header" 1429dd9d0cfSchristosThe fields of the ELF executable header that need to be set by the 1439dd9d0cfSchristosapplication are: 1449dd9d0cfSchristos.Pp 1459dd9d0cfSchristos.Bl -tag -width "e_ident[EI_OSABI]" -compact 1469dd9d0cfSchristos.It Va e_entry 1479dd9d0cfSchristosTo be set to the desired entry address for executables. 1489dd9d0cfSchristos.It Va e_flags 1499dd9d0cfSchristosTo be set to the desired processor specific flags. 1509dd9d0cfSchristos.It Va "e_ident[EI_DATA]" 1519dd9d0cfSchristosMust be set to one of 1529dd9d0cfSchristos.Dv ELFDATA2LSB 1539dd9d0cfSchristosor 1549dd9d0cfSchristos.Dv ELFDATA2MSB . 1559dd9d0cfSchristos.It Va "e_ident[EI_OSABI]" 1569dd9d0cfSchristosTo be set to the OS ABI desired. 1579dd9d0cfSchristosFor example, for 1589dd9d0cfSchristos.Fx 1599dd9d0cfSchristosexecutables, this field should be set to 1609dd9d0cfSchristos.Dv ELFOSABI_FREEBSD . 1619dd9d0cfSchristos.It Va e_machine 1629dd9d0cfSchristosTo be set to the desired machine architecture, one of the 1639dd9d0cfSchristos.Dv EM_* 1649dd9d0cfSchristosvalues in the header file 1659dd9d0cfSchristos.In elfdefinitions.h . 1669dd9d0cfSchristos.It Va e_phoff 1679dd9d0cfSchristosIf the application is managing the object's layout, it must 1689dd9d0cfSchristosset this field to the file offset of the ELF program header table. 1699dd9d0cfSchristos.It Va e_shoff 1709dd9d0cfSchristosIf the application is managing the object's layout, it must 1719dd9d0cfSchristosset this field to the file offset of the ELF section header table. 1729dd9d0cfSchristos.It Va e_shstrndx 1739dd9d0cfSchristosTo be set to the index of the string table containing 1749dd9d0cfSchristossection names. 1759dd9d0cfSchristos.It Va e_type 1769dd9d0cfSchristosTo be set to the type of the ELF object, one of the 1779dd9d0cfSchristos.Dv ET_* 1789dd9d0cfSchristosvalues in the header file 1799dd9d0cfSchristos.In elfdefinitions.h . 1809dd9d0cfSchristos.It Va e_version 1819dd9d0cfSchristosTo be set to the desired version of the ELF object. 1829dd9d0cfSchristos.El 1839dd9d0cfSchristos.It "Program Header" 1849dd9d0cfSchristosAll fields of the entries in the program header table need to be 1859dd9d0cfSchristosset by the application. 1869dd9d0cfSchristos.It "Section Header" 1879dd9d0cfSchristosThe fields of ELF section headers that need to be set by the 1889dd9d0cfSchristosapplication are: 1899dd9d0cfSchristos.Pp 1909dd9d0cfSchristos.Bl -tag -width "sh_addralign" -compact 1919dd9d0cfSchristos.It Va sh_addr 1929dd9d0cfSchristosTo be set to the memory address where the section should reside. 1939dd9d0cfSchristos.It Va sh_addralign 1949dd9d0cfSchristosIf the application is managing the file layout, it must set this 1959dd9d0cfSchristosfield to the desired alignment for the section's contents. 1969dd9d0cfSchristosThis value must be a power of two and must be at least as large as the 1979dd9d0cfSchristoslargest alignment needed by any 1989dd9d0cfSchristos.Vt Elf_Data 1999dd9d0cfSchristosdescriptor associated with the section. 2009dd9d0cfSchristos.It Va sh_entsize 2019dd9d0cfSchristosTo be set to the size of each entry, for sections containing fixed size 2029dd9d0cfSchristoselements, or set to zero for sections without fixed size elements. 2039dd9d0cfSchristosIf the application is not managing file layout, it may leave this 2049dd9d0cfSchristosfield as zero for those sections whose types are known to the library. 2059dd9d0cfSchristos.It Va sh_flags 2069dd9d0cfSchristosTo be set to the desired section flags. 2079dd9d0cfSchristos.It Va sh_info 2089dd9d0cfSchristosTo be set as described in 2099dd9d0cfSchristos.Xr elf 5 . 2109dd9d0cfSchristos.It Va sh_link 2119dd9d0cfSchristosTo be set as described in 2129dd9d0cfSchristos.Xr elf 5 . 2139dd9d0cfSchristos.It Va sh_name 2149dd9d0cfSchristosTo be set to the index of the section's name in the string table 2159dd9d0cfSchristoscontaining section names. 2169dd9d0cfSchristos.It Va sh_offset 2179dd9d0cfSchristosIf the application is managing the file layout, it must set this 2189dd9d0cfSchristosfield to the file offset of the section's contents. 2199dd9d0cfSchristos.It Va sh_size 2209dd9d0cfSchristosIf the application is managing the file layout, it must set this 2219dd9d0cfSchristosfield to the file size of the section's contents. 2229dd9d0cfSchristos.It Va sh_type 2239dd9d0cfSchristosTo be set to the type of the section. 2249dd9d0cfSchristos.El 2259dd9d0cfSchristos.It "Section Data" 2269dd9d0cfSchristosThe 2279dd9d0cfSchristos.Vt Elf_Data 2289dd9d0cfSchristosdescriptors associated with each section specify its contents 2299dd9d0cfSchristos(see 2309dd9d0cfSchristos.Xr elf_getdata 3 ) . 2319dd9d0cfSchristosWhile all the fields in these descriptors are under application 2329dd9d0cfSchristoscontrol, the following fields influence object layout: 2339dd9d0cfSchristos.Bl -tag -width "Va d_align" -compact 2349dd9d0cfSchristos.It Va d_align 2359dd9d0cfSchristosTo be set to the desired alignment, within the containing section, of 2369dd9d0cfSchristosthe descriptor's data. 2379dd9d0cfSchristos.It Va d_off 2389dd9d0cfSchristosIf the application is managing object layout, it must set this field 2399dd9d0cfSchristosto the file offset, within the section, at which the descriptor's data 2409dd9d0cfSchristosshould be placed. 2419dd9d0cfSchristos.It Va d_size 2429dd9d0cfSchristosTo be set to the size in bytes of the memory representation of the 2439dd9d0cfSchristosdescriptor's data. 2449dd9d0cfSchristos.El 2459dd9d0cfSchristos.El 2469dd9d0cfSchristos.Sh RETURN VALUES 2479dd9d0cfSchristosFunction 2489dd9d0cfSchristos.Fn elf_update 2499dd9d0cfSchristosreturns the total size of the file image if successful, or -1 if an 2509dd9d0cfSchristoserror occurred. 2519dd9d0cfSchristos.Sh ERRORS 2529dd9d0cfSchristosThis function may fail with the following errors: 2539dd9d0cfSchristos.Bl -tag -width "[ELF_E_RESOURCE]" 2549dd9d0cfSchristos.It Bq Er ELF_E_ARGUMENT 2559dd9d0cfSchristosArgument 2563f622274Sjkoshy.Fa elf 2579dd9d0cfSchristoswas null. 2589dd9d0cfSchristos.It Bq Er ELF_E_ARGUMENT 2599dd9d0cfSchristosArgument 2603f622274Sjkoshy.Fa cmd 2619dd9d0cfSchristoswas not recognized. 2629dd9d0cfSchristos.It Bq Er ELF_E_ARGUMENT 2639dd9d0cfSchristosThe argument 2643f622274Sjkoshy.Fa elf 2659dd9d0cfSchristoswas not a descriptor for an ELF object. 2669dd9d0cfSchristos.It Bq Er ELF_E_CLASS 2679dd9d0cfSchristosThe 2689dd9d0cfSchristos.Va e_ident[EI_CLASS] 2699dd9d0cfSchristosfield of the executable header of argument 2703f622274Sjkoshy.Fa elf 2719dd9d0cfSchristosdid not match the class of the file. 2729dd9d0cfSchristos.It Bq Er ELF_E_DATA 2739dd9d0cfSchristosAn 2749dd9d0cfSchristos.Vt Elf_Data 2759dd9d0cfSchristosdescriptor contained in argument 2763f622274Sjkoshy.Fa elf 2779dd9d0cfSchristosspecified an unsupported type. 2789dd9d0cfSchristos.It Bq Er ELF_E_DATA 2799dd9d0cfSchristosAn 2809dd9d0cfSchristos.Vt Elf_Data 2819dd9d0cfSchristosdescriptor specified an alignment that was zero or was not a power of 2829dd9d0cfSchristostwo. 2839dd9d0cfSchristos.It Bq Er ELF_E_HEADER 2849dd9d0cfSchristosThe ELF header in argument 2853f622274Sjkoshy.Fa elf 2869dd9d0cfSchristosrequested a different byte order from the byte order already 2879dd9d0cfSchristosassociated with the file. 2889dd9d0cfSchristos.It Bq Er ELF_E_IO 2899dd9d0cfSchristosAn I/O error was encountered. 2909dd9d0cfSchristos.It Bq Er ELF_E_LAYOUT 2919dd9d0cfSchristosAn 2929dd9d0cfSchristos.Vt Elf_Data 2939dd9d0cfSchristosdescriptor contained in argument 2943f622274Sjkoshy.Fa elf 2959dd9d0cfSchristosspecified an alignment incompatible with its containing section. 2969dd9d0cfSchristos.It Bq Er ELF_E_LAYOUT 2979dd9d0cfSchristosArgument 2983f622274Sjkoshy.Fa elf 2999dd9d0cfSchristoscontained section descriptors that overlapped in extent. 3009dd9d0cfSchristos.It Bq Er ELF_E_LAYOUT 3019dd9d0cfSchristosArgument 3023f622274Sjkoshy.Fa elf 3039dd9d0cfSchristoscontained section descriptors that were incorrectly aligned or were 3049dd9d0cfSchristostoo small for their data. 3059dd9d0cfSchristos.It Bq Er ELF_E_LAYOUT 3069dd9d0cfSchristosThe flag 3079dd9d0cfSchristos.Dv ELF_F_LAYOUT 3089dd9d0cfSchristoswas set on the Elf descriptor and the executable header overlapped 3099dd9d0cfSchristoswith the program header table. 3109dd9d0cfSchristos.It Bq Er ELF_E_LAYOUT 3119dd9d0cfSchristosThe flag 3129dd9d0cfSchristos.Dv ELF_F_LAYOUT 3139dd9d0cfSchristoswas set on the Elf descriptor and the program header table was placed 3149dd9d0cfSchristosat a misaligned file offset. 3159dd9d0cfSchristos.It Bq Er ELF_E_LAYOUT 3169dd9d0cfSchristosThe flag 3179dd9d0cfSchristos.Dv ELF_F_LAYOUT 3189dd9d0cfSchristoswas set on the Elf descriptor and the section header table overlapped 3199dd9d0cfSchristosan extent mapped by a section descriptor. 3209dd9d0cfSchristos.It Bq Er ELF_E_LAYOUT 3219dd9d0cfSchristosThe 3229dd9d0cfSchristos.Dv ELF_F_LAYOUT 3239dd9d0cfSchristosflag was set on the Elf descriptor, and the 3249dd9d0cfSchristos.Va d_offset 3259dd9d0cfSchristosfield in an 3269dd9d0cfSchristos.Vt Elf_Data 3279dd9d0cfSchristosdescriptor contained a value that was not a multiple of the 3289dd9d0cfSchristosdescriptor's specified alignment. 3299dd9d0cfSchristos.It Bq Er ELF_E_MODE 3309dd9d0cfSchristosAn 3319dd9d0cfSchristos.Dv ELF_C_WRITE 3329dd9d0cfSchristosoperation was requested with an ELF descriptor that was not opened for 3339dd9d0cfSchristoswriting or updating. 3349dd9d0cfSchristos.It Bq Er ELF_E_SECTION 3359dd9d0cfSchristosArgument 3363f622274Sjkoshy.Fa elf 3379dd9d0cfSchristoscontained a section with an unrecognized type. 3389dd9d0cfSchristos.It Bq Er ELF_E_SECTION 3399dd9d0cfSchristosThe section header at index 3409dd9d0cfSchristos.Dv SHN_UNDEF 3419dd9d0cfSchristoshad an illegal section type. 3429dd9d0cfSchristos.It Bq Er ELF_E_SEQUENCE 3439dd9d0cfSchristosAn 3449dd9d0cfSchristos.Dv ELF_C_WRITE 3459dd9d0cfSchristosoperation was requested after a prior call to 3469dd9d0cfSchristos.Fn elf_cntl elf ELF_C_FDDONE 3479dd9d0cfSchristosdisassociated the ELF descriptor 3483f622274Sjkoshy.Fa elf 3499dd9d0cfSchristosfrom its underlying file. 350c5261b83Sjkoshy.It Bq Er ELF_E_UNIMPL 351c5261b83SjkoshyArgument 3523f622274Sjkoshy.Fa elf 353c5261b83Sjkoshycontained a section with an unsupported ELF type. 3549dd9d0cfSchristos.It Bq Er ELF_E_VERSION 3559dd9d0cfSchristosArgument 3563f622274Sjkoshy.Fa elf 3579dd9d0cfSchristoshad an unsupported version or contained an 3589dd9d0cfSchristos.Vt Elf_Data 3599dd9d0cfSchristosdescriptor with an unsupported version. 3609dd9d0cfSchristos.El 3619dd9d0cfSchristos.Sh SEE ALSO 3629dd9d0cfSchristos.Xr elf 3 , 3639dd9d0cfSchristos.Xr elf32_getehdr 3 , 3649dd9d0cfSchristos.Xr elf32_getphdr 3 , 3659dd9d0cfSchristos.Xr elf32_newehdr 3 , 3669dd9d0cfSchristos.Xr elf32_newphdr 3 , 3679dd9d0cfSchristos.Xr elf64_getehdr 3 , 3689dd9d0cfSchristos.Xr elf64_getphdr 3 , 3699dd9d0cfSchristos.Xr elf64_newehdr 3 , 3709dd9d0cfSchristos.Xr elf64_newphdr 3 , 3719dd9d0cfSchristos.Xr elf_begin 3 , 3729dd9d0cfSchristos.Xr elf_cntl 3 , 3739dd9d0cfSchristos.Xr elf_fill 3 , 3749dd9d0cfSchristos.Xr elf_flagehdr 3 , 3759dd9d0cfSchristos.Xr elf_flagelf 3 , 3769dd9d0cfSchristos.Xr elf_getdata 3 , 3779dd9d0cfSchristos.Xr elf_getscn 3 , 3789dd9d0cfSchristos.Xr elf_newdata 3 , 3799dd9d0cfSchristos.Xr elf_newscn 3 , 3809dd9d0cfSchristos.Xr elf_rawdata 3 , 3819dd9d0cfSchristos.Xr gelf 3 , 3829dd9d0cfSchristos.Xr gelf_newehdr 3 , 3839dd9d0cfSchristos.Xr gelf_newphdr 3 , 3849dd9d0cfSchristos.Xr elf 5 385