@@ -11,7 +11,6 @@ import (
11
11
"fmt"
12
12
"io"
13
13
"os"
14
- "slices"
15
14
"time"
16
15
17
16
"github.com/siderolabs/gen/xslices"
@@ -112,7 +111,16 @@ func AssembleNative(srcPath, dstPath string, sections []Section) error {
112
111
newHeader .MinorLinkerVersion = 0
113
112
newHeader .CheckSum = 0
114
113
115
- newSections := slices .Clone (peFile .Sections )
114
+ type peSectionWithPath struct {
115
+ pe.Section
116
+ SourcePath string
117
+ }
118
+
119
+ newSections := xslices .Map (peFile .Sections , func (section * pe.Section ) * peSectionWithPath {
120
+ return & peSectionWithPath {
121
+ Section : * section ,
122
+ }
123
+ })
116
124
117
125
// calculate sections size and VMA
118
126
for i := range sections {
@@ -133,14 +141,17 @@ func AssembleNative(srcPath, dstPath string, sections []Section) error {
133
141
134
142
newFileHeader .NumberOfSections ++
135
143
136
- newSections = append (newSections , & pe.Section {
137
- SectionHeader : pe.SectionHeader {
138
- Name : sections [i ].Name ,
139
- VirtualSize : uint32 (sections [i ].virtualSize ),
140
- VirtualAddress : uint32 (sections [i ].virtualAddress ),
141
- Size : uint32 ((sections [i ].virtualSize + fileAlignment ) &^ fileAlignment ),
142
- Characteristics : pe .IMAGE_SCN_CNT_INITIALIZED_DATA | pe .IMAGE_SCN_MEM_READ ,
144
+ newSections = append (newSections , & peSectionWithPath {
145
+ Section : pe.Section {
146
+ SectionHeader : pe.SectionHeader {
147
+ Name : sections [i ].Name ,
148
+ VirtualSize : uint32 (sections [i ].virtualSize ),
149
+ VirtualAddress : uint32 (sections [i ].virtualAddress ),
150
+ Size : uint32 ((sections [i ].virtualSize + fileAlignment ) &^ fileAlignment ),
151
+ Characteristics : pe .IMAGE_SCN_CNT_INITIALIZED_DATA | pe .IMAGE_SCN_MEM_READ ,
152
+ },
143
153
},
154
+ SourcePath : sections [i ].Path ,
144
155
})
145
156
}
146
157
@@ -174,7 +185,7 @@ func AssembleNative(srcPath, dstPath string, sections []Section) error {
174
185
}
175
186
176
187
// 3. Section headers
177
- rawSections := xslices .Map (newSections , func (section * pe. Section ) pe.SectionHeader32 {
188
+ rawSections := xslices .Map (newSections , func (section * peSectionWithPath ) pe.SectionHeader32 {
178
189
var rawName [8 ]byte
179
190
180
191
copy (rawName [:], section .Name )
@@ -206,22 +217,19 @@ func AssembleNative(srcPath, dstPath string, sections []Section) error {
206
217
// 4. Section data
207
218
for i , rawSection := range rawSections {
208
219
name := newSections [i ].Name
220
+ sourcePath := newSections [i ].SourcePath
209
221
210
- if err := func (rawSection pe.SectionHeader32 , name string ) error {
222
+ if err := func (rawSection pe.SectionHeader32 , name , sourcePath string ) error {
211
223
// the section might come either from the input PE file or from a separate file
212
224
var sectionData io.ReadCloser
213
225
214
- for _ , section := range sections {
215
- if section .Append && section .Name == name {
216
- sectionData , err = os .Open (section .Path )
217
- if err != nil {
218
- return fmt .Errorf ("failed to open section data: %w" , err )
219
- }
220
-
221
- defer sectionData .Close () //nolint: errcheck
222
-
223
- break
226
+ if sourcePath != "" {
227
+ sectionData , err = os .Open (sourcePath )
228
+ if err != nil {
229
+ return fmt .Errorf ("failed to open section data: %w" , err )
224
230
}
231
+
232
+ defer sectionData .Close () //nolint: errcheck
225
233
}
226
234
227
235
if sectionData == nil {
@@ -260,7 +268,7 @@ func AssembleNative(srcPath, dstPath string, sections []Section) error {
260
268
}
261
269
262
270
return nil
263
- }(rawSection , name ); err != nil {
271
+ }(rawSection , name , sourcePath ); err != nil {
264
272
return fmt .Errorf ("failed to write section data %s: %w" , name , err )
265
273
}
266
274
}
0 commit comments