@@ -122,3 +122,65 @@ func (orbisElf *OrbisElf) RewriteInterpreter(interpreter string) error {
122122 _ , err = orbisElf .FinalFile .WriteAt (interpreterBuff , rewriteOffset )
123123 return err
124124}
125+
126+ // RewriteDynamicSectionHeader will overwrite the address of the .dynamic section with the given address. Returns
127+ // an error if the write failed, nil otherwise.
128+ func (orbisElf * OrbisElf ) RewriteDynamicSectionHeader () error {
129+ var (
130+ inputFile * os.File
131+ err error
132+ )
133+
134+ // Get the section header offset info from the original file
135+ inputHdr := new (elf.Header64 )
136+
137+ if inputFile , err = os .Open (orbisElf .ElfToConvertName ); err != nil {
138+ return err
139+ }
140+
141+ if _ , err = inputFile .Seek (0 , io .SeekStart ); err != nil {
142+ return err
143+ }
144+
145+ if err = binary .Read (inputFile , orbisElf .ElfToConvert .ByteOrder , inputHdr ); err != nil {
146+ return err
147+ }
148+
149+ sectionHeadersOffset := inputHdr .Shoff
150+
151+ // Find the dynamic header
152+ for i := uint16 (0 ); i < inputHdr .Shnum ; i ++ {
153+ sectionHdr := new (elf.Section64 )
154+ sectionHeaderOffset := int64 (sectionHeadersOffset + uint64 (i * inputHdr .Shentsize ))
155+
156+ if _ , err = inputFile .Seek (sectionHeaderOffset , io .SeekStart ); err != nil {
157+ return err
158+ }
159+
160+ if err = binary .Read (inputFile , orbisElf .ElfToConvert .ByteOrder , sectionHdr ); err != nil {
161+ return err
162+ }
163+
164+ if sectionHdr .Type == uint32 (elf .SHT_DYNAMIC ) {
165+ sectionHeaderBuff := new (bytes.Buffer )
166+
167+ // Rewrite the address
168+ sectionHdr .Off = _offsetOfDynamic
169+ sectionHdr .Addr = _offsetOfDynamic
170+ sectionHdr .Size = _sizeOfDynamic
171+
172+ // Commit the write
173+ if err := binary .Write (sectionHeaderBuff , binary .LittleEndian , sectionHdr ); err != nil {
174+ return err
175+ }
176+
177+ if _ , err := orbisElf .FinalFile .WriteAt (sectionHeaderBuff .Bytes (), sectionHeaderOffset ); err != nil {
178+ return err
179+ }
180+
181+ break
182+ }
183+ }
184+
185+ return nil
186+ }
0 commit comments