Skip to content

Commit fa87e70

Browse files
committed
Merge remote-tracking branch
'origin/GP-0_ryanmkurtz_PR-7957_peterbelm_omf51-libfs' (Closes #7957)
2 parents 7c1285d + 8701d22 commit fa87e70

File tree

10 files changed

+622
-3
lines changed

10 files changed

+622
-3
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/* ###
2+
* IP: GHIDRA
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package ghidra.app.util.bin.format.omf.omf51;
17+
18+
import java.io.IOException;
19+
import java.util.ArrayList;
20+
import java.util.List;
21+
22+
import ghidra.app.util.bin.format.omf.OmfException;
23+
import ghidra.app.util.bin.format.omf.OmfString;
24+
25+
public class Omf51Library {
26+
27+
private List<MemberHeader> members = new ArrayList<>();
28+
29+
public record MemberHeader(long offset, long size, String name) {}
30+
31+
/**
32+
* Creates a new {@link Omf51Library}
33+
*
34+
* @param factory A {@link Omf51RecordFactory}
35+
* @throws IOException if an IO-related error occurred
36+
* @throws OmfException if the required OMF-51 records could not be read
37+
*/
38+
public Omf51Library(Omf51RecordFactory factory) throws OmfException, IOException {
39+
if (!(factory.readNextRecord() instanceof Omf51LibraryHeaderRecord libraryHeader)) {
40+
throw new OmfException("Unable to read library header record");
41+
}
42+
43+
factory.getReader().setPointerIndex(libraryHeader.getModNamesOffset());
44+
45+
if (!(factory.readNextRecord() instanceof Omf51LibraryModuleNamesRecord modNamesRecord)) {
46+
throw new OmfException("Unable to read library module names record");
47+
}
48+
49+
if (!(factory.readNextRecord() instanceof Omf51LibraryModuleLocationsRecord modLocations)) {
50+
throw new OmfException("Unable to read library module locations record");
51+
}
52+
53+
List<Omf51LibraryModuleLocation> locations = modLocations.getLocations();
54+
55+
int index = 0;
56+
for (OmfString moduleName : modNamesRecord.getNames()) {
57+
int currentOffset = locations.get(index).getOffset();
58+
int nextOffset = index + 1 < locations.size() ? locations.get(index + 1).getOffset()
59+
: libraryHeader.getModNamesOffset();
60+
int size = nextOffset - currentOffset;
61+
members.add(new MemberHeader(currentOffset, size, moduleName.str()));
62+
index++;
63+
}
64+
}
65+
66+
/**
67+
* {@return the list of members}
68+
*/
69+
public List<MemberHeader> getMembers() {
70+
return members;
71+
}
72+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/* ###
2+
* IP: GHIDRA
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package ghidra.app.util.bin.format.omf.omf51;
17+
18+
import java.io.IOException;
19+
import java.util.ArrayList;
20+
import java.util.List;
21+
22+
import ghidra.app.util.bin.BinaryReader;
23+
import ghidra.app.util.bin.format.omf.*;
24+
import ghidra.program.model.data.*;
25+
import ghidra.util.exception.DuplicateNameException;
26+
27+
public class Omf51LibraryDictionaryRecord extends OmfRecord {
28+
29+
private List<List<OmfString>> moduleSymbolMap = new ArrayList<>();
30+
31+
/**
32+
* Creates a new {@link Omf51LibraryDictionaryRecord}
33+
*
34+
* @param reader A {@link BinaryReader} positioned at the start of the record
35+
* @throws IOException if an IO-related error occurred
36+
*/
37+
public Omf51LibraryDictionaryRecord(BinaryReader reader) throws IOException {
38+
super(reader);
39+
}
40+
41+
@Override
42+
public void parseData() throws IOException, OmfException {
43+
List<OmfString> symbols = new ArrayList<>();
44+
45+
while (dataReader.getPointerIndex() < dataEnd) {
46+
byte len = dataReader.peekNextByte();
47+
if (len == 0) {
48+
dataReader.readNextByte();
49+
moduleSymbolMap.add(symbols);
50+
symbols = new ArrayList<>();
51+
}
52+
else {
53+
symbols.add(OmfUtils.readString(dataReader));
54+
}
55+
}
56+
}
57+
58+
@Override
59+
public DataType toDataType() throws DuplicateNameException, IOException {
60+
StructureDataType struct = new StructureDataType(Omf51RecordTypes.getName(recordType), 0);
61+
struct.add(BYTE, "type", null);
62+
struct.add(WORD, "length", null);
63+
Integer moduleIndex = 0;
64+
for (List<OmfString> symbols : moduleSymbolMap) {
65+
for (OmfString symbol : symbols) {
66+
struct.add(symbol.toDataType(), symbol.getDataTypeSize(),
67+
"symbol%d".formatted(moduleIndex), null);
68+
}
69+
70+
struct.add(BYTE, "terminator%d".formatted(moduleIndex), null);
71+
moduleIndex++;
72+
}
73+
struct.add(BYTE, "checksum", null);
74+
75+
struct.setCategoryPath(new CategoryPath(OmfUtils.CATEGORY_PATH));
76+
return struct;
77+
}
78+
79+
/**
80+
* {@return the symbol names partitioned by module}
81+
*/
82+
public List<List<OmfString>> getModuleSymbolMap() {
83+
return moduleSymbolMap;
84+
}
85+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/* ###
2+
* IP: GHIDRA
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package ghidra.app.util.bin.format.omf.omf51;
17+
18+
import java.io.IOException;
19+
20+
import ghidra.app.util.bin.BinaryReader;
21+
import ghidra.app.util.bin.format.omf.*;
22+
import ghidra.program.model.data.*;
23+
import ghidra.util.exception.DuplicateNameException;
24+
25+
public class Omf51LibraryHeaderRecord extends OmfRecord {
26+
27+
public static final int BLOCK_SIZE = 128;
28+
29+
private short moduleCount;
30+
private short blockNumber;
31+
private short byteNumber;
32+
33+
/**
34+
* Creates a new {@link Omf51LibraryHeaderRecord} record
35+
*
36+
* @param reader A {@link BinaryReader} positioned at the start of the record
37+
* @throws IOException if an IO-related error occurred
38+
*/
39+
public Omf51LibraryHeaderRecord(BinaryReader reader) throws IOException {
40+
super(reader);
41+
}
42+
43+
@Override
44+
public void parseData() throws IOException, OmfException {
45+
moduleCount = dataReader.readNextShort();
46+
blockNumber = dataReader.readNextShort();
47+
byteNumber = dataReader.readNextShort();
48+
}
49+
50+
@Override
51+
public DataType toDataType() throws DuplicateNameException, IOException {
52+
StructureDataType struct = new StructureDataType(Omf51RecordTypes.getName(recordType), 0);
53+
struct.add(BYTE, "type", null);
54+
struct.add(WORD, "length", null);
55+
struct.add(WORD, "moduleCount", null);
56+
struct.add(WORD, "blockNumber", null);
57+
struct.add(WORD, "byteNumber", null);
58+
struct.add(BYTE, "checksum", null);
59+
60+
struct.setCategoryPath(new CategoryPath(OmfUtils.CATEGORY_PATH));
61+
return struct;
62+
}
63+
64+
/**
65+
* {@return the module count}
66+
*/
67+
public short getModuleCount() {
68+
return moduleCount;
69+
}
70+
71+
/**
72+
* {@return the module names block number}
73+
*/
74+
public short getModNamesBlockNumber() {
75+
return blockNumber;
76+
}
77+
78+
/**
79+
* {@return the module names byte number}
80+
*/
81+
public short getModNamesByteNumber() {
82+
return byteNumber;
83+
}
84+
85+
/**
86+
* {@return the module names file offset}
87+
*/
88+
public int getModNamesOffset() {
89+
return (blockNumber * BLOCK_SIZE) + byteNumber;
90+
}
91+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/* ###
2+
* IP: GHIDRA
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package ghidra.app.util.bin.format.omf.omf51;
17+
18+
import java.io.IOException;
19+
20+
import ghidra.app.util.bin.BinaryReader;
21+
import ghidra.app.util.bin.StructConverter;
22+
import ghidra.app.util.bin.format.omf.OmfUtils;
23+
import ghidra.program.model.data.*;
24+
25+
public class Omf51LibraryModuleLocation {
26+
27+
public static final int BLOCK_SIZE = 128;
28+
29+
private int blockNumber;
30+
private int byteNumber;
31+
32+
/**
33+
* Creates a new {@link Omf51LibraryModuleLocation}
34+
*
35+
* @param reader A {@link BinaryReader} positioned at the start of the segment definition
36+
* @throws IOException if an IO-related error occurred
37+
*/
38+
public Omf51LibraryModuleLocation(BinaryReader reader) throws IOException {
39+
blockNumber = reader.readNextUnsignedShort();
40+
byteNumber = reader.readNextUnsignedShort();
41+
}
42+
43+
/**
44+
* {@return the block number}
45+
*/
46+
public int getBlockNumber() {
47+
return blockNumber;
48+
}
49+
50+
/**
51+
* {@return the byte number}
52+
*/
53+
public int getByteNumber() {
54+
return byteNumber;
55+
}
56+
57+
/**
58+
* {@return the offset into the library}
59+
*/
60+
public int getOffset() {
61+
return (blockNumber * BLOCK_SIZE) + byteNumber;
62+
}
63+
64+
public static DataType toDataType() {
65+
StructureDataType struct = new StructureDataType("Omf51LibraryModuleLocation", 0);
66+
struct.add(StructConverter.WORD, "blockNumber", null);
67+
struct.add(StructConverter.WORD, "byteNumber", null);
68+
struct.setCategoryPath(new CategoryPath(OmfUtils.CATEGORY_PATH));
69+
return struct;
70+
}
71+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/* ###
2+
* IP: GHIDRA
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package ghidra.app.util.bin.format.omf.omf51;
17+
18+
import java.io.IOException;
19+
import java.util.ArrayList;
20+
import java.util.List;
21+
22+
import ghidra.app.util.bin.BinaryReader;
23+
import ghidra.app.util.bin.format.omf.*;
24+
import ghidra.program.model.data.*;
25+
import ghidra.util.exception.DuplicateNameException;
26+
27+
public class Omf51LibraryModuleLocationsRecord extends OmfRecord {
28+
29+
private List<Omf51LibraryModuleLocation> locations = new ArrayList<>();
30+
31+
/**
32+
* Creates a new {@link Omf51LibraryModuleLocationsRecord} record
33+
*
34+
* @param reader A {@link BinaryReader} positioned at the start of the record
35+
* @throws IOException if an IO-related error occurred
36+
*/
37+
public Omf51LibraryModuleLocationsRecord(BinaryReader reader) throws IOException {
38+
super(reader);
39+
}
40+
41+
@Override
42+
public void parseData() throws IOException, OmfException {
43+
while (dataReader.getPointerIndex() < dataEnd) {
44+
locations.add(new Omf51LibraryModuleLocation(dataReader));
45+
}
46+
}
47+
48+
@Override
49+
public DataType toDataType() throws DuplicateNameException, IOException {
50+
StructureDataType struct = new StructureDataType(Omf51RecordTypes.getName(recordType), 0);
51+
struct.add(BYTE, "type", null);
52+
struct.add(WORD, "length", null);
53+
struct.add(new ArrayDataType(Omf51LibraryModuleLocation.toDataType(), locations.size()),
54+
"locations", null);
55+
struct.add(BYTE, "checksum", null);
56+
57+
struct.setCategoryPath(new CategoryPath(OmfUtils.CATEGORY_PATH));
58+
return struct;
59+
}
60+
61+
/**
62+
* {@return the list of module locations}
63+
*/
64+
public List<Omf51LibraryModuleLocation> getLocations() {
65+
return locations;
66+
}
67+
}

0 commit comments

Comments
 (0)