+ /**
+ * Strip the leading zeroes from the address
+ * */
+ private static String stripLeadingZeros(String address) {
+ return address.replaceFirst("^0+(?!$)", ""); //$NON-NLS-1$ //$NON-NLS-2$;
+ }
+
+ public static @Nullable Map<String, String> mapFromBinaryFile(File file) {
+ Map<String, String> map = new HashMap<>();
+ IBinaryParser.IBinaryObject binaryObject = getBinaryObject(file);
+ if (binaryObject != null) {
+ ISymbol[] symbols = binaryObject.getSymbols();
+ for (ISymbol symbol : symbols) {
+ String address = symbol.getAddress().toHexAddressString();
+ /* Remove "0x" */
+ address = address.substring(2);
+ /* Strip the leading zeroes from the address */
+ address = stripLeadingZeros(address);
+ map.put(address, symbol.getName());
+ }
+ }
+
+ return map;
+ }
+
+ private static @Nullable IBinaryParser.IBinaryObject getBinaryObject(File file) {
+ IPath filePath = new Path(file.toString());
+
+ /* Get all the available binary parsers */
+ final List<IBinaryParser> binaryParsers = new ArrayList<>();
+ IConfigurationElement[] elements = Platform.getExtensionRegistry()
+ .getConfigurationElementsFor(CCorePlugin.BINARY_PARSER_UNIQ_ID);
+ for (IConfigurationElement element : elements) {
+ IConfigurationElement[] children = element.getChildren("run"); //$NON-NLS-1$
+ for (final IConfigurationElement run : children) {
+ SafeRunner.run(new ISafeRunnable() {
+ @Override
+ public void run() throws Exception {
+ IBinaryParser binaryParser = (IBinaryParser) run.createExecutableExtension("class"); //$NON-NLS-1$
+ binaryParsers.add(binaryParser);
+ }
+
+ @Override
+ public void handleException(Throwable exception) {
+ Activator.getDefault().logError("Error creating binary parser", exception); //$NON-NLS-1$
+ }
+ });
+ }
+ }
+
+ /* Find the maximum "hint" buffer size we'll need from all the parsers */
+ int hintBufferSize = 0;
+ for (IBinaryParser parser : binaryParsers) {
+ if (parser.getHintBufferSize() > hintBufferSize) {
+ hintBufferSize = Math.max(hintBufferSize, parser.getHintBufferSize());
+ }
+ }
+
+ /* Read the initial "hint" bytes */
+ byte[] hintBuffer = new byte[hintBufferSize];
+ if (hintBufferSize > 0) {
+ try (InputStream is = new FileInputStream(file) ){
+
+ int count = 0;
+ // Make sure we read up to 'hints' bytes if we possibly can
+ while (count < hintBufferSize) {
+ int bytesRead = is.read(hintBuffer, count, hintBufferSize - count);
+ if (bytesRead < 0) {
+ break;
+ }
+ count += bytesRead;
+ }
+ if (count > 0 && count < hintBuffer.length) {
+ byte[] array = new byte[count];
+ System.arraycopy(hintBuffer, 0, array, 0, count);
+ hintBuffer = array;
+ }
+ } catch (IOException e) {
+ Activator.getDefault().logError("Error reading initial bytes of binary file", e); //$NON-NLS-1$
+ return null;
+ }
+ }
+
+ /* For all binary parsers, try to get a binary object */
+ for (IBinaryParser parser : binaryParsers) {
+ if (parser.isBinary(hintBuffer, filePath)) {
+ IBinaryFile binFile;
+ try {
+ binFile = parser.getBinary(hintBuffer, filePath);
+ if (binFile != null && binFile instanceof IBinaryParser.IBinaryObject) {
+ return (IBinaryParser.IBinaryObject)binFile;
+ }
+ } catch (IOException e) {
+ Activator.getDefault().logError("Error parsing binary file", e); //$NON-NLS-1$
+ }
+ }
+ }
+
+ return null;
+ }
+