xref: /freebsd/contrib/elftoolchain/libelf/elf_update.3 (revision ae500c1ff8974130f7f2692772cf288b90349e0d)
12de3b87aSKai Wang.\" Copyright (c) 2006-2011 Joseph Koshy.  All rights reserved.
22de3b87aSKai Wang.\"
32de3b87aSKai Wang.\" Redistribution and use in source and binary forms, with or without
42de3b87aSKai Wang.\" modification, are permitted provided that the following conditions
52de3b87aSKai Wang.\" are met:
62de3b87aSKai Wang.\" 1. Redistributions of source code must retain the above copyright
72de3b87aSKai Wang.\"    notice, this list of conditions and the following disclaimer.
82de3b87aSKai Wang.\" 2. Redistributions in binary form must reproduce the above copyright
92de3b87aSKai Wang.\"    notice, this list of conditions and the following disclaimer in the
102de3b87aSKai Wang.\"    documentation and/or other materials provided with the distribution.
112de3b87aSKai Wang.\"
122de3b87aSKai Wang.\" This software is provided by Joseph Koshy ``as is'' and
132de3b87aSKai Wang.\" any express or implied warranties, including, but not limited to, the
142de3b87aSKai Wang.\" implied warranties of merchantability and fitness for a particular purpose
152de3b87aSKai Wang.\" are disclaimed.  in no event shall Joseph Koshy be liable
162de3b87aSKai Wang.\" for any direct, indirect, incidental, special, exemplary, or consequential
172de3b87aSKai Wang.\" damages (including, but not limited to, procurement of substitute goods
182de3b87aSKai Wang.\" or services; loss of use, data, or profits; or business interruption)
192de3b87aSKai Wang.\" however caused and on any theory of liability, whether in contract, strict
202de3b87aSKai Wang.\" liability, or tort (including negligence or otherwise) arising in any way
212de3b87aSKai Wang.\" out of the use of this software, even if advised of the possibility of
222de3b87aSKai Wang.\" such damage.
232de3b87aSKai Wang.\"
24*ae500c1fSEd Maste.\" $Id: elf_update.3 3639 2018-10-14 14:07:02Z jkoshy $
252de3b87aSKai Wang.\"
262de3b87aSKai Wang.Dd August 14, 2011
272de3b87aSKai Wang.Dt ELF_UPDATE 3
28*ae500c1fSEd Maste.Os
292de3b87aSKai Wang.Sh NAME
302de3b87aSKai Wang.Nm elf_update
312de3b87aSKai Wang.Nd update an ELF descriptor
322de3b87aSKai Wang.Sh LIBRARY
332de3b87aSKai Wang.Lb libelf
342de3b87aSKai Wang.Sh SYNOPSIS
352de3b87aSKai Wang.In libelf.h
362de3b87aSKai Wang.Ft off_t
372de3b87aSKai Wang.Fn elf_update "Elf *elf" "Elf_Cmd cmd"
382de3b87aSKai Wang.Sh DESCRIPTION
392de3b87aSKai WangFunction
402de3b87aSKai Wang.Fn elf_update
412de3b87aSKai Wangcauses the library to recalculate the structure of an ELF
422de3b87aSKai Wangobject and optionally write out the image of the object
432de3b87aSKai Wangto file.
442de3b87aSKai Wang.Pp
452de3b87aSKai WangArgument
462de3b87aSKai Wang.Ar elf
472de3b87aSKai Wangshould reference a valid ELF descriptor.
482de3b87aSKai Wang.Pp
492de3b87aSKai WangArgument
502de3b87aSKai Wang.Ar cmd
512de3b87aSKai Wangcan be one of the following values:
522de3b87aSKai Wang.Bl -tag -width "Dv ELF_C_WRITE"
532de3b87aSKai Wang.It Dv ELF_C_NULL
542de3b87aSKai WangThe library will recalculate structural information flagging
552de3b87aSKai Wangmodified structures with the
562de3b87aSKai Wang.Dv ELF_F_DIRTY
572de3b87aSKai Wangflag, but will not write data to the underlying file image.
582de3b87aSKai Wang.It Dv ELF_C_WRITE
592de3b87aSKai WangThe library will recalculate structural information and will
602de3b87aSKai Wangalso write the new image to the underlying file.
612de3b87aSKai WangThe ELF descriptor referenced by argument
622de3b87aSKai Wang.Ar elf
632de3b87aSKai Wangshould permit the underlying ELF object to be written or updated
642de3b87aSKai Wang(see
652de3b87aSKai Wang.Xr elf_begin 3 ) .
662de3b87aSKai Wang.El
672de3b87aSKai Wang.Pp
682de3b87aSKai WangAll pointers to
692de3b87aSKai Wang.Vt Elf_Scn
702de3b87aSKai Wangand
712de3b87aSKai Wang.Vt Elf_Data
722de3b87aSKai Wangdescriptors associated with descriptor
732de3b87aSKai Wang.Ar elf
742de3b87aSKai Wangshould be considered invalid after a call to
752de3b87aSKai Wang.Fn elf_update .
762de3b87aSKai Wang.Ss Specifying Object Layout
772de3b87aSKai WangThe
782de3b87aSKai Wang.Lb libelf
792de3b87aSKai Wangsupports two layout modes.
802de3b87aSKai Wang.Bl -tag -width indent
812de3b87aSKai Wang.It "Library Layout"
822de3b87aSKai WangIf the
832de3b87aSKai Wang.Dv ELF_F_LAYOUT
842de3b87aSKai Wangflag is not set on the ELF descriptor, the ELF library will lay out
852de3b87aSKai Wangthe ELF object according to the following scheme:
862de3b87aSKai Wang.Bl -tag -compact -width "Section Data"
872de3b87aSKai Wang.It Em EHDR
882de3b87aSKai WangThe ELF executable header will be placed at the start of the object.
892de3b87aSKai Wang.It Em PHDR
902de3b87aSKai WangIf the ELF descriptor contains a program header table, it will be
912de3b87aSKai Wangplaced after the Executable Header.
922de3b87aSKai Wang.It Em Section Data
932de3b87aSKai WangELF section data, if any, will be placed next, keeping each section's
942de3b87aSKai Wangalignment requirements in mind.
952de3b87aSKai Wang.It Em SHDR
962de3b87aSKai WangThe ELF section header table, if any, will be placed last.
972de3b87aSKai Wang.El
982de3b87aSKai Wang.It "Application Controlled Layout"
992de3b87aSKai WangThe application can take full control of the layout of the ELF object
1002de3b87aSKai Wangby setting the
1012de3b87aSKai Wang.Dv ELF_F_LAYOUT
1022de3b87aSKai Wangflag on the ELF descriptor (see
1032de3b87aSKai Wang.Xr elf_flagelf 3 ) .
1042de3b87aSKai WangIn this case the library will lay out the ELF object using
1052de3b87aSKai Wangapplication-supplied information as below:
1062de3b87aSKai Wang.Pp
1072de3b87aSKai Wang.Bl -tag -compact -width "Section Data"
1082de3b87aSKai Wang.It Em EHDR
1092de3b87aSKai WangThe ELF executable header will be placed at the start of the object.
1102de3b87aSKai Wang.It Em PHDR
1112de3b87aSKai WangThe ELF program header table, if any, it will be placed at the offset
1122de3b87aSKai Wangspecified in the
1132de3b87aSKai Wang.Va e_phoff
1142de3b87aSKai Wangfield of the ELF executable header.
1152de3b87aSKai Wang.It Em Section Data
1162de3b87aSKai WangThe data for each ELF section will be placed at the offset specified
1172de3b87aSKai Wangby the
1182de3b87aSKai Wang.Va sh_offset
1192de3b87aSKai Wangfield of the section's header.
1202de3b87aSKai WangThe size of the section will be taken from the
1212de3b87aSKai Wang.Va sh_size
1222de3b87aSKai Wangfield of the section header.
1232de3b87aSKai Wang.It Em SHDR
1242de3b87aSKai WangThe ELF section header table, if any, will be placed at the offset
1252de3b87aSKai Wangspecified by the
1262de3b87aSKai Wang.Va e_shoff
1272de3b87aSKai Wangfield of the executable header.
1282de3b87aSKai Wang.El
1292de3b87aSKai Wang.El
1302de3b87aSKai Wang.Pp
1312de3b87aSKai WangGaps in the coverage of the file's contents will be set to the fill value
1322de3b87aSKai Wangspecified by
1332de3b87aSKai Wang.Xr elf_fill 3 .
1342de3b87aSKai Wang.Ss Application Supplied Information
1352de3b87aSKai WangThe application needs to set the following fields in the data
1362de3b87aSKai Wangstructures associated with the ELF descriptor prior to calling
1372de3b87aSKai Wang.Fn elf_update .
1382de3b87aSKai Wang.Bl -tag -width indent
1392de3b87aSKai Wang.It "Executable Header"
1402de3b87aSKai WangThe fields of the ELF executable header that need to be set by the
1412de3b87aSKai Wangapplication are:
1422de3b87aSKai Wang.Pp
1432de3b87aSKai Wang.Bl -tag -width "e_ident[EI_OSABI]" -compact
1442de3b87aSKai Wang.It Va e_entry
1452de3b87aSKai WangTo be set to the desired entry address for executables.
1462de3b87aSKai Wang.It Va e_flags
1472de3b87aSKai WangTo be set to the desired processor specific flags.
1482de3b87aSKai Wang.It Va "e_ident[EI_DATA]"
1492de3b87aSKai WangMust be set to one of
1502de3b87aSKai Wang.Dv ELFDATA2LSB
1512de3b87aSKai Wangor
1522de3b87aSKai Wang.Dv ELFDATA2MSB .
1532de3b87aSKai Wang.It Va "e_ident[EI_OSABI]"
1542de3b87aSKai WangTo be set to the OS ABI desired.
1552de3b87aSKai WangFor example, for
1562de3b87aSKai Wang.Fx
1572de3b87aSKai Wangexecutables, this field should be set to
1582de3b87aSKai Wang.Dv ELFOSABI_FREEBSD .
1592de3b87aSKai Wang.It Va e_machine
1602de3b87aSKai WangTo be set to the desired machine architecture, one of the
1612de3b87aSKai Wang.Dv EM_*
1622de3b87aSKai Wangvalues in the header file
1632de3b87aSKai Wang.In elfdefinitions.h .
1642de3b87aSKai Wang.It Va e_phoff
1652de3b87aSKai WangIf the application is managing the object's layout, it must
1662de3b87aSKai Wangset this field to the file offset of the ELF program header table.
1672de3b87aSKai Wang.It Va e_shoff
1682de3b87aSKai WangIf the application is managing the object's layout, it must
1692de3b87aSKai Wangset this field to the file offset of the ELF section header table.
1702de3b87aSKai Wang.It Va e_shstrndx
1712de3b87aSKai WangTo be set to the index of the string table containing
1722de3b87aSKai Wangsection names.
1732de3b87aSKai Wang.It Va e_type
1742de3b87aSKai WangTo be set to the type of the ELF object, one of the
1752de3b87aSKai Wang.Dv ET_*
1762de3b87aSKai Wangvalues in the header file
1772de3b87aSKai Wang.In elfdefinitions.h .
1782de3b87aSKai Wang.It Va e_version
1792de3b87aSKai WangTo be set to the desired version of the ELF object.
1802de3b87aSKai Wang.El
1812de3b87aSKai Wang.It "Program Header"
1822de3b87aSKai WangAll fields of the entries in the program header table need to be
1832de3b87aSKai Wangset by the application.
1842de3b87aSKai Wang.It "Section Header"
1852de3b87aSKai WangThe fields of ELF section headers that need to be set by the
1862de3b87aSKai Wangapplication are:
1872de3b87aSKai Wang.Pp
1882de3b87aSKai Wang.Bl -tag -width "sh_addralign" -compact
1892de3b87aSKai Wang.It Va sh_addr
1902de3b87aSKai WangTo be set to the memory address where the section should reside.
1912de3b87aSKai Wang.It Va sh_addralign
1922de3b87aSKai WangIf the application is managing the file layout, it must set this
1932de3b87aSKai Wangfield to the desired alignment for the section's contents.
1942de3b87aSKai WangThis value must be a power of two and must be at least as large as the
1952de3b87aSKai Wanglargest alignment needed by any
1962de3b87aSKai Wang.Vt Elf_Data
1972de3b87aSKai Wangdescriptor associated with the section.
1982de3b87aSKai Wang.It Va sh_entsize
1992de3b87aSKai WangTo be set to the size of each entry, for sections containing fixed size
2002de3b87aSKai Wangelements, or set to zero for sections without fixed size elements.
2012de3b87aSKai WangIf the application is not managing file layout, it may leave this
2022de3b87aSKai Wangfield as zero for those sections whose types are known to the library.
2032de3b87aSKai Wang.It Va sh_flags
2042de3b87aSKai WangTo be set to the desired section flags.
2052de3b87aSKai Wang.It Va sh_info
2062de3b87aSKai WangTo be set as described in
2072de3b87aSKai Wang.Xr elf 5 .
2082de3b87aSKai Wang.It Va sh_link
2092de3b87aSKai WangTo be set as described in
2102de3b87aSKai Wang.Xr elf 5 .
2112de3b87aSKai Wang.It Va sh_name
2122de3b87aSKai WangTo be set to the index of the section's name in the string table
2132de3b87aSKai Wangcontaining section names.
2142de3b87aSKai Wang.It Va sh_offset
2152de3b87aSKai WangIf the application is managing the file layout, it must set this
2162de3b87aSKai Wangfield to the file offset of the section's contents.
2172de3b87aSKai Wang.It Va sh_size
2182de3b87aSKai WangIf the application is managing the file layout, it must set this
2192de3b87aSKai Wangfield to the file size of the section's contents.
2202de3b87aSKai Wang.It Va sh_type
2212de3b87aSKai WangTo be set to the type of the section.
2222de3b87aSKai Wang.El
2232de3b87aSKai Wang.It "Section Data"
2242de3b87aSKai WangThe
2252de3b87aSKai Wang.Vt Elf_Data
2262de3b87aSKai Wangdescriptors associated with each section specify its contents
2272de3b87aSKai Wang(see
2282de3b87aSKai Wang.Xr elf_getdata 3 ) .
2292de3b87aSKai WangWhile all the fields in these descriptors are under application
2302de3b87aSKai Wangcontrol, the following fields influence object layout:
2312de3b87aSKai Wang.Bl -tag -width "Va d_align" -compact
2322de3b87aSKai Wang.It Va d_align
2332de3b87aSKai WangTo be set to the desired alignment, within the containing section, of
2342de3b87aSKai Wangthe descriptor's data.
2352de3b87aSKai Wang.It Va d_off
2362de3b87aSKai WangIf the application is managing object layout, it must set this field
2372de3b87aSKai Wangto the file offset, within the section, at which the descriptor's data
2382de3b87aSKai Wangshould be placed.
2392de3b87aSKai Wang.It Va d_size
2402de3b87aSKai WangTo be set to the size in bytes of the memory representation of the
2412de3b87aSKai Wangdescriptor's data.
2422de3b87aSKai Wang.El
2432de3b87aSKai Wang.El
2442de3b87aSKai Wang.Sh RETURN VALUES
2452de3b87aSKai WangFunction
2462de3b87aSKai Wang.Fn elf_update
2472de3b87aSKai Wangreturns the total size of the file image if successful, or -1 if an
2482de3b87aSKai Wangerror occurred.
2492de3b87aSKai Wang.Sh ERRORS
2502de3b87aSKai WangThis function may fail with the following errors:
2512de3b87aSKai Wang.Bl -tag -width "[ELF_E_RESOURCE]"
2522de3b87aSKai Wang.It Bq Er ELF_E_ARGUMENT
2532de3b87aSKai WangArgument
2542de3b87aSKai Wang.Ar elf
2552de3b87aSKai Wangwas null.
2562de3b87aSKai Wang.It Bq Er ELF_E_ARGUMENT
2572de3b87aSKai WangArgument
2582de3b87aSKai Wang.Ar cmd
2592de3b87aSKai Wangwas not recognized.
2602de3b87aSKai Wang.It Bq Er ELF_E_ARGUMENT
2612de3b87aSKai WangThe argument
2622de3b87aSKai Wang.Ar elf
2632de3b87aSKai Wangwas not a descriptor for an ELF object.
2642de3b87aSKai Wang.It Bq Er ELF_E_CLASS
2652de3b87aSKai WangThe
2662de3b87aSKai Wang.Va e_ident[EI_CLASS]
2672de3b87aSKai Wangfield of the executable header of argument
2682de3b87aSKai Wang.Ar elf
2692de3b87aSKai Wangdid not match the class of the file.
2702de3b87aSKai Wang.It Bq Er ELF_E_DATA
2712de3b87aSKai WangAn
2722de3b87aSKai Wang.Vt Elf_Data
2732de3b87aSKai Wangdescriptor contained in argument
2742de3b87aSKai Wang.Ar elf
2752de3b87aSKai Wangspecified an unsupported type.
2762de3b87aSKai Wang.It Bq Er ELF_E_DATA
2772de3b87aSKai WangAn
2782de3b87aSKai Wang.Vt Elf_Data
2792de3b87aSKai Wangdescriptor specified an alignment that was zero or was not a power of
2802de3b87aSKai Wangtwo.
2812de3b87aSKai Wang.It Bq Er ELF_E_HEADER
2822de3b87aSKai WangThe ELF header in argument
2832de3b87aSKai Wang.Ar elf
2842de3b87aSKai Wangrequested a different byte order from the byte order already
2852de3b87aSKai Wangassociated with the file.
2862de3b87aSKai Wang.It Bq Er ELF_E_IO
2872de3b87aSKai WangAn I/O error was encountered.
2882de3b87aSKai Wang.It Bq Er ELF_E_LAYOUT
2892de3b87aSKai WangAn
2902de3b87aSKai Wang.Vt Elf_Data
2912de3b87aSKai Wangdescriptor contained in argument
2922de3b87aSKai Wang.Ar elf
2932de3b87aSKai Wangspecified an alignment incompatible with its containing section.
2942de3b87aSKai Wang.It Bq Er ELF_E_LAYOUT
2952de3b87aSKai WangArgument
2962de3b87aSKai Wang.Ar elf
2972de3b87aSKai Wangcontained section descriptors that overlapped in extent.
2982de3b87aSKai Wang.It Bq Er ELF_E_LAYOUT
2992de3b87aSKai WangArgument
3002de3b87aSKai Wang.Ar elf
3012de3b87aSKai Wangcontained section descriptors that were incorrectly aligned or were
3022de3b87aSKai Wangtoo small for their data.
3032de3b87aSKai Wang.It Bq Er ELF_E_LAYOUT
3042de3b87aSKai WangThe flag
3052de3b87aSKai Wang.Dv ELF_F_LAYOUT
3062de3b87aSKai Wangwas set on the Elf descriptor and the executable header overlapped
3072de3b87aSKai Wangwith the program header table.
3082de3b87aSKai Wang.It Bq Er ELF_E_LAYOUT
3092de3b87aSKai WangThe flag
3102de3b87aSKai Wang.Dv ELF_F_LAYOUT
3112de3b87aSKai Wangwas set on the Elf descriptor and the program header table was placed
3122de3b87aSKai Wangat a misaligned file offset.
3132de3b87aSKai Wang.It Bq Er ELF_E_LAYOUT
3142de3b87aSKai WangThe flag
3152de3b87aSKai Wang.Dv ELF_F_LAYOUT
3162de3b87aSKai Wangwas set on the Elf descriptor and the section header table overlapped
3172de3b87aSKai Wangan extent mapped by a section descriptor.
3182de3b87aSKai Wang.It Bq Er ELF_E_LAYOUT
3192de3b87aSKai WangThe
3202de3b87aSKai Wang.Dv ELF_F_LAYOUT
3212de3b87aSKai Wangflag was set on the Elf descriptor, and the
3222de3b87aSKai Wang.Va d_offset
3232de3b87aSKai Wangfield in an
3242de3b87aSKai Wang.Vt Elf_Data
3252de3b87aSKai Wangdescriptor contained a value that was not a multiple of the
3262de3b87aSKai Wangdescriptor's specified alignment.
3272de3b87aSKai Wang.It Bq Er ELF_E_MODE
3282de3b87aSKai WangAn
3292de3b87aSKai Wang.Dv ELF_C_WRITE
3302de3b87aSKai Wangoperation was requested with an ELF descriptor that was not opened for
3312de3b87aSKai Wangwriting or updating.
3322de3b87aSKai Wang.It Bq Er ELF_E_SECTION
3332de3b87aSKai WangArgument
3342de3b87aSKai Wang.Ar elf
3352de3b87aSKai Wangcontained a section with an unrecognized type.
3362de3b87aSKai Wang.It Bq Er ELF_E_SECTION
3372de3b87aSKai WangThe section header at index
3382de3b87aSKai Wang.Dv SHN_UNDEF
3392de3b87aSKai Wanghad an illegal section type.
3402de3b87aSKai Wang.It Bq Er ELF_E_SEQUENCE
3412de3b87aSKai WangAn
3422de3b87aSKai Wang.Dv ELF_C_WRITE
3432de3b87aSKai Wangoperation was requested after a prior call to
3442de3b87aSKai Wang.Fn elf_cntl elf ELF_C_FDDONE
3452de3b87aSKai Wangdisassociated the ELF descriptor
3462de3b87aSKai Wang.Ar elf
3472de3b87aSKai Wangfrom its underlying file.
3482de3b87aSKai Wang.It Bq Er ELF_E_VERSION
3492de3b87aSKai WangArgument
3502de3b87aSKai Wang.Ar elf
3512de3b87aSKai Wanghad an unsupported version or contained an
3522de3b87aSKai Wang.Vt Elf_Data
3532de3b87aSKai Wangdescriptor with an unsupported version.
3542de3b87aSKai Wang.El
3552de3b87aSKai Wang.Sh SEE ALSO
3562de3b87aSKai Wang.Xr elf 3 ,
3572de3b87aSKai Wang.Xr elf32_getehdr 3 ,
3582de3b87aSKai Wang.Xr elf32_getphdr 3 ,
3592de3b87aSKai Wang.Xr elf32_newehdr 3 ,
3602de3b87aSKai Wang.Xr elf32_newphdr 3 ,
3612de3b87aSKai Wang.Xr elf64_getehdr 3 ,
3622de3b87aSKai Wang.Xr elf64_getphdr 3 ,
3632de3b87aSKai Wang.Xr elf64_newehdr 3 ,
3642de3b87aSKai Wang.Xr elf64_newphdr 3 ,
3652de3b87aSKai Wang.Xr elf_begin 3 ,
3662de3b87aSKai Wang.Xr elf_cntl 3 ,
3672de3b87aSKai Wang.Xr elf_fill 3 ,
3682de3b87aSKai Wang.Xr elf_flagehdr 3 ,
3692de3b87aSKai Wang.Xr elf_flagelf 3 ,
3702de3b87aSKai Wang.Xr elf_getdata 3 ,
3712de3b87aSKai Wang.Xr elf_getscn 3 ,
3722de3b87aSKai Wang.Xr elf_newdata 3 ,
3732de3b87aSKai Wang.Xr elf_newscn 3 ,
3742de3b87aSKai Wang.Xr elf_rawdata 3 ,
3752de3b87aSKai Wang.Xr gelf 3 ,
3762de3b87aSKai Wang.Xr gelf_newehdr 3 ,
3772de3b87aSKai Wang.Xr gelf_newphdr 3 ,
3782de3b87aSKai Wang.Xr elf 5
379