summaryrefslogtreecommitdiff
path: root/cmake/elfinfo
diff options
context:
space:
mode:
Diffstat (limited to 'cmake/elfinfo')
-rw-r--r--cmake/elfinfo/src/main/java/io/trygvis/ld/ElfinfoExprVisitor.java318
-rw-r--r--cmake/elfinfo/src/main/java/io/trygvis/ld/ElfinfoGnuLdListener.java170
-rw-r--r--cmake/elfinfo/src/main/java/io/trygvis/ld/ElfinfoGnuLdVisitor.java75
-rw-r--r--cmake/elfinfo/src/main/java/io/trygvis/ld/LdLoader.java121
-rw-r--r--cmake/elfinfo/src/main/java/io/trygvis/ld/LdScript.java498
-rw-r--r--cmake/elfinfo/src/main/test/io/trygvis/ld/FullScriptsTest.java6
6 files changed, 687 insertions, 501 deletions
diff --git a/cmake/elfinfo/src/main/java/io/trygvis/ld/ElfinfoExprVisitor.java b/cmake/elfinfo/src/main/java/io/trygvis/ld/ElfinfoExprVisitor.java
new file mode 100644
index 0000000..af5bea5
--- /dev/null
+++ b/cmake/elfinfo/src/main/java/io/trygvis/ld/ElfinfoExprVisitor.java
@@ -0,0 +1,318 @@
+package io.trygvis.ld;
+
+import io.trygvis.ld.antlr.GnuLdParser;
+import io.trygvis.ld.antlr.GnuLdParserBaseVisitor;
+import org.antlr.v4.runtime.tree.ParseTreeProperty;
+
+import java.math.BigInteger;
+
+class ElfinfoExprVisitor extends GnuLdParserBaseVisitor<ElfinfoExprVisitor> {
+ private BigInteger value;
+ private ParseTreeProperty<BigInteger> es = new ParseTreeProperty<>();
+
+ public static BigInteger evaluate(GnuLdParser.Mustbe_expContext ctx) {
+ ElfinfoExprVisitor v = new ElfinfoExprVisitor();
+ v.visitMustbe_exp(ctx);
+ return v.value();
+ }
+
+ @Override
+ public ElfinfoExprVisitor visitExpLt(GnuLdParser.ExpLtContext ctx) {
+ return visitChildren(ctx);
+ }
+
+ @Override
+ public ElfinfoExprVisitor visitExpBlock(GnuLdParser.ExpBlockContext ctx) {
+ return visitChildren(ctx);
+
+ }
+
+ @Override
+ public ElfinfoExprVisitor visitExpAbsolute(GnuLdParser.ExpAbsoluteContext ctx) {
+ return visitChildren(ctx);
+
+ }
+
+ @Override
+ public ElfinfoExprVisitor visitExpLog2ceil(GnuLdParser.ExpLog2ceilContext ctx) {
+ return visitChildren(ctx);
+
+ }
+
+ @Override
+ public ElfinfoExprVisitor visitExpAddr(GnuLdParser.ExpAddrContext ctx) {
+ return visitChildren(ctx);
+
+ }
+
+ @Override
+ public ElfinfoExprVisitor visitExpDataSegmentEnd(GnuLdParser.ExpDataSegmentEndContext ctx) {
+ return visitChildren(ctx);
+ }
+
+ @Override
+ public ElfinfoExprVisitor visitExpSub(GnuLdParser.ExpSubContext ctx) {
+ System.out.println("ElfinfoExprVisitor.visitExpSub");
+
+ ElfinfoExprVisitor ret = visitChildren(ctx);
+ BigInteger a = es.removeFrom(ctx.exp(0));
+ BigInteger b = es.removeFrom(ctx.exp(1));
+
+ BigInteger x = a.subtract(b);
+
+ es.put(ctx, x);
+ return ret;
+ }
+
+ @Override
+ public ElfinfoExprVisitor visitExpDefined(GnuLdParser.ExpDefinedContext ctx) {
+ return visitChildren(ctx);
+ }
+
+ @Override
+ public ElfinfoExprVisitor visitExpMod(GnuLdParser.ExpModContext ctx) {
+ return visitChildren(ctx);
+ }
+
+ @Override
+ public ElfinfoExprVisitor visitExpInvert(GnuLdParser.ExpInvertContext ctx) {
+ return visitChildren(ctx);
+ }
+
+ @Override
+ public ElfinfoExprVisitor visitExpAlign(GnuLdParser.ExpAlignContext ctx) {
+ return visitChildren(ctx);
+ }
+
+ @Override
+ public ElfinfoExprVisitor visitExpMul(GnuLdParser.ExpMulContext ctx) {
+ return visitChildren(ctx);
+ }
+
+ @Override
+ public ElfinfoExprVisitor visitExpAnd(GnuLdParser.ExpAndContext ctx) {
+ return visitChildren(ctx);
+ }
+
+ @Override
+ public ElfinfoExprVisitor visitExpXor(GnuLdParser.ExpXorContext ctx) {
+ return visitChildren(ctx);
+ }
+
+ @Override
+ public ElfinfoExprVisitor visitExpParen(GnuLdParser.ExpParenContext ctx) {
+ return visitChildren(ctx);
+ }
+
+ @Override
+ public ElfinfoExprVisitor visitExpMinus(GnuLdParser.ExpMinusContext ctx) {
+ return visitChildren(ctx);
+ }
+
+ @Override
+ public ElfinfoExprVisitor visitExpDiv(GnuLdParser.ExpDivContext ctx) {
+ return visitChildren(ctx);
+ }
+
+ @Override
+ public ElfinfoExprVisitor visitExpGe(GnuLdParser.ExpGeContext ctx) {
+ return visitChildren(ctx);
+ }
+
+ @Override
+ public ElfinfoExprVisitor visitExpMin(GnuLdParser.ExpMinContext ctx) {
+ return visitChildren(ctx);
+ }
+
+ @Override
+ public ElfinfoExprVisitor visitExpAlignK(GnuLdParser.ExpAlignKContext ctx) {
+ return visitChildren(ctx);
+ }
+
+ @Override
+ public ElfinfoExprVisitor visitExpNegate(GnuLdParser.ExpNegateContext ctx) {
+ return visitChildren(ctx);
+ }
+
+ @Override
+ public ElfinfoExprVisitor visitExpName(GnuLdParser.ExpNameContext ctx) {
+ return visitChildren(ctx);
+ }
+
+ @Override
+ public ElfinfoExprVisitor visitExpOr(GnuLdParser.ExpOrContext ctx) {
+ return visitChildren(ctx);
+ }
+
+ @Override
+ public ElfinfoExprVisitor visitExpTrinary(GnuLdParser.ExpTrinaryContext ctx) {
+ return visitChildren(ctx);
+ }
+
+ @Override
+ public ElfinfoExprVisitor visitExpOror(GnuLdParser.ExpOrorContext ctx) {
+ return visitChildren(ctx);
+ }
+
+ @Override
+ public ElfinfoExprVisitor visitExpDataSegmentAlign(GnuLdParser.ExpDataSegmentAlignContext ctx) {
+ return visitChildren(ctx);
+ }
+
+ @Override
+ public ElfinfoExprVisitor visitExpLengthExp(GnuLdParser.ExpLengthExpContext ctx) {
+ return visitChildren(ctx);
+ }
+
+ @Override
+ public ElfinfoExprVisitor visitExpAdd(GnuLdParser.ExpAddContext ctx) {
+ return visitChildren(ctx);
+ }
+
+ @Override
+ public ElfinfoExprVisitor visitExpLoadaddr(GnuLdParser.ExpLoadaddrContext ctx) {
+ return visitChildren(ctx);
+ }
+
+ @Override
+ public ElfinfoExprVisitor visitExpGt(GnuLdParser.ExpGtContext ctx) {
+ return visitChildren(ctx);
+ }
+
+ @Override
+ public ElfinfoExprVisitor visitExpOrigin(GnuLdParser.ExpOriginContext ctx) {
+ return visitChildren(ctx);
+ }
+
+ @Override
+ public ElfinfoExprVisitor visitExpEq(GnuLdParser.ExpEqContext ctx) {
+ return visitChildren(ctx);
+ }
+
+ @Override
+ public ElfinfoExprVisitor visitExpMax(GnuLdParser.ExpMaxContext ctx) {
+ return visitChildren(ctx);
+ }
+
+ @Override
+ public ElfinfoExprVisitor visitExpLshift(GnuLdParser.ExpLshiftContext ctx) {
+ return visitChildren(ctx);
+ }
+
+ @Override
+ public ElfinfoExprVisitor visitExpSizeofHeaders(GnuLdParser.ExpSizeofHeadersContext ctx) {
+ return visitChildren(ctx);
+ }
+
+ @Override
+ public ElfinfoExprVisitor visitExpLe(GnuLdParser.ExpLeContext ctx) {
+ return visitChildren(ctx);
+ }
+
+ @Override
+ public ElfinfoExprVisitor visitExpNe(GnuLdParser.ExpNeContext ctx) {
+ return visitChildren(ctx);
+ }
+
+ @Override
+ public ElfinfoExprVisitor visitExpAndand(GnuLdParser.ExpAndandContext ctx) {
+ return visitChildren(ctx);
+ }
+
+ @Override
+ public ElfinfoExprVisitor visitExpRshift(GnuLdParser.ExpRshiftContext ctx) {
+ return visitChildren(ctx);
+ }
+
+ @Override
+ public ElfinfoExprVisitor visitExpNextParen(GnuLdParser.ExpNextParenContext ctx) {
+ return visitChildren(ctx);
+ }
+
+ @Override
+ public ElfinfoExprVisitor visitExpAlignof(GnuLdParser.ExpAlignofContext ctx) {
+ return visitChildren(ctx);
+ }
+
+ @Override
+ public ElfinfoExprVisitor visitExpSegmentStart(GnuLdParser.ExpSegmentStartContext ctx) {
+ return visitChildren(ctx);
+ }
+
+ @Override
+ public ElfinfoExprVisitor visitExpInt(GnuLdParser.ExpIntContext ctx) {
+ System.out.println("ElfinfoExprVisitor.visitExpInt");
+
+ BigInteger i = parseInt(ctx.INT().getText());
+ es.put(ctx, i);
+
+ return this;
+ }
+
+ public static BigInteger parseInt(String str) {
+ str = str.toLowerCase();
+ int base = 10;
+ if (str.startsWith("0x")) {
+ base = 16;
+ str = str.substring(2);
+ }
+
+ int factor = 1;
+ if (str.endsWith("k")) {
+ factor = 1024;
+ str = str.substring(0, str.length() - 1);
+ } else if (str.endsWith("k")) {
+ factor = 1024 * 1024;
+ str = str.substring(0, str.length() - 1);
+ }
+
+ BigInteger i = new BigInteger(str, base);
+ if (factor > 1) {
+ i = i.multiply(BigInteger.valueOf(factor));
+ }
+ return i;
+ }
+
+ @Override
+ public ElfinfoExprVisitor visitExpConstant(GnuLdParser.ExpConstantContext ctx) {
+ return visitChildren(ctx);
+ }
+
+ @Override
+ public ElfinfoExprVisitor visitExpPlus(GnuLdParser.ExpPlusContext ctx) {
+ return visitChildren(ctx);
+ }
+
+ @Override
+ public ElfinfoExprVisitor visitExpSizeof(GnuLdParser.ExpSizeofContext ctx) {
+ return visitChildren(ctx);
+ }
+
+ @Override
+ public ElfinfoExprVisitor visitExpDataSegmentRelRoEnd(GnuLdParser.ExpDataSegmentRelRoEndContext ctx) {
+ return visitChildren(ctx);
+ }
+
+ @Override
+ public ElfinfoExprVisitor visitExpAssert(GnuLdParser.ExpAssertContext ctx) {
+ return visitChildren(ctx);
+ }
+
+ @Override
+ public ElfinfoExprVisitor visitMustbe_exp(GnuLdParser.Mustbe_expContext ctx) {
+ ElfinfoExprVisitor ret = visitChildren(ctx);
+
+ value = es.removeFrom(ctx.exp());
+ es.put(ctx, value);
+
+ return ret;
+ }
+
+ public BigInteger value() {
+ if (value != null) {
+ return value;
+ }
+
+ throw new RuntimeException("Something bad happened, probably an unevaluated expression part.");
+ }
+}
diff --git a/cmake/elfinfo/src/main/java/io/trygvis/ld/ElfinfoGnuLdListener.java b/cmake/elfinfo/src/main/java/io/trygvis/ld/ElfinfoGnuLdListener.java
new file mode 100644
index 0000000..fe40547
--- /dev/null
+++ b/cmake/elfinfo/src/main/java/io/trygvis/ld/ElfinfoGnuLdListener.java
@@ -0,0 +1,170 @@
+package io.trygvis.ld;
+
+import io.trygvis.ld.antlr.GnuLdParser;
+import io.trygvis.ld.antlr.GnuLdParserBaseListener;
+import org.antlr.v4.runtime.tree.ParseTreeProperty;
+
+import java.math.BigInteger;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+class ElfinfoGnuLdListener extends GnuLdParserBaseListener {
+ public List<LdLoader.MemoryArea> memoryAreas = new ArrayList<>();
+
+ private final ParseTreeProperty<BigInteger> expr = new ParseTreeProperty<>();
+
+ @Override
+ public void exitExpInt(GnuLdParser.ExpIntContext ctx) {
+// System.out.println("ElfinfoGnuLdListener.exitExpInt: ctx.INT().getText() = " + ctx.INT().getText());
+ expr.put(ctx, ElfinfoExprVisitor.parseInt(ctx.INT().getText()));
+ }
+
+ @Override
+ public void exitExpSub(GnuLdParser.ExpSubContext ctx) {
+ BigInteger a = expr.get(ctx.exp(0));
+ BigInteger b = expr.get(ctx.exp(1));
+ BigInteger x = a.subtract(b);
+ expr.put(ctx, x);
+ }
+
+ @Override
+ public void exitExpAdd(GnuLdParser.ExpAddContext ctx) {
+ BigInteger a = expr.get(ctx.exp(0));
+ BigInteger b = expr.get(ctx.exp(1));
+ BigInteger x = a.add(b);
+ expr.put(ctx, x);
+ }
+
+ @Override
+ public void exitExpName(GnuLdParser.ExpNameContext ctx) {
+ expr.put(ctx, BigInteger.ZERO);
+ }
+
+ @Override
+ public void exitExpAddr(GnuLdParser.ExpAddrContext ctx) {
+ expr.put(ctx, BigInteger.ZERO);
+ }
+
+ @Override
+ public void exitExpSizeof(GnuLdParser.ExpSizeofContext ctx) {
+ expr.put(ctx, BigInteger.ZERO);
+ }
+
+ @Override
+ public void exitExpLengthExp(GnuLdParser.ExpLengthExpContext ctx) {
+ LdLoader.MemoryArea ma = getMemoryArea(ctx.NAME().getText());
+// System.out.println("ma.length = " + ma.length);
+ expr.put(ctx, ma.length);
+ }
+
+ @Override
+ public void exitExpOrigin(GnuLdParser.ExpOriginContext ctx) {
+// System.out.println("ElfinfoGnuLdListener.exitExpOrigin: " + ctx.getText());
+ LdLoader.MemoryArea ma = getMemoryArea(ctx.NAME().getText());
+// System.out.println("ma.origin = " + ma.origin);
+ expr.put(ctx, ma.origin);
+ }
+
+ private LdLoader.MemoryArea getMemoryArea(String name) {
+ LdLoader.MemoryArea m = null;
+ for (LdLoader.MemoryArea ma : memoryAreas) {
+ if (ma.name.equals(name)) {
+ m = ma;
+ }
+ }
+ if (m == null) {
+ throw new RuntimeException("No such memory area: " + name);
+ }
+ return m;
+ }
+
+ @Override
+ public void enterMustbe_exp(GnuLdParser.Mustbe_expContext ctx) {
+// System.out.println("ElfinfoGnuLdListener.enterMustbe_exp");
+ }
+
+ @Override
+ public void exitMustbe_exp(GnuLdParser.Mustbe_expContext ctx) {
+// System.out.println("ElfinfoGnuLdListener.exitMustbe_exp");
+
+ expr.put(ctx, expr.get(ctx.exp()));
+ }
+
+
+ @Override
+ public void enterOrigin_spec(GnuLdParser.Origin_specContext ctx) {
+// System.out.println("ElfinfoGnuLdListener.enterOrigin_spec");
+ }
+
+ @Override
+ public void exitOrigin_spec(GnuLdParser.Origin_specContext ctx) {
+// System.out.println("ElfinfoGnuLdListener.exitOrigin_spec");
+ }
+
+ @Override
+ public void enterLength_spec(GnuLdParser.Length_specContext ctx) {
+// System.out.println("ElfinfoGnuLdListener.enterLength_spec");
+ }
+
+ @Override
+ public void exitLength_spec(GnuLdParser.Length_specContext ctx) {
+// System.out.println("ElfinfoGnuLdListener.exitLength_spec");
+ }
+
+ @Override
+ public void enterMemory_spec(GnuLdParser.Memory_specContext ctx) {
+// System.out.println("ElfinfoGnuLdListener.enterMemory_spec");
+ }
+
+ @Override
+ public void exitMemory_spec(GnuLdParser.Memory_specContext ctx) {
+// System.out.println("ElfinfoGnuLdListener.exitMemory_spec");
+ LdLoader.MemoryArea ma = new LdLoader.MemoryArea();
+ ma.name = ctx.NAME().getText();
+ ma.attributes = attributes;
+// System.out.println("ctx.origin_spec() = " + ctx.origin_spec());
+ ma.origin = expr.get(ctx.origin_spec().mustbe_exp());
+ ma.length = expr.get(ctx.length_spec().mustbe_exp());
+ System.out.println(ma);
+ memoryAreas.add(ma);
+ }
+
+ LdLoader.MemoryAttribute attribute;
+ boolean attributesInverted;
+ Set<LdLoader.MemoryAttribute> attributes;
+
+ @Override
+ public void exitAttributes_opt(GnuLdParser.Attributes_optContext ctx) {
+// System.out.println("ElfinfoGnuLdListener.exitAttributes_opt");
+ attributes = new HashSet<>();
+ }
+
+ @Override
+ public void enterAttributeInverted(GnuLdParser.AttributeInvertedContext ctx) {
+// System.out.println("ElfinfoGnuLdListener.enterAttributeInverted");
+
+ if (!attributes.isEmpty()) {
+ throw new RuntimeException("Attributes for memory areas can only be attributesInverted (with '!') as the first character in a specification; foo(!rw), not foo(x!rw).");
+ }
+// String name = ctx.name().getText();
+ String name = ctx.NAME().getText();
+// System.out.println("ctx.ATTRIBUTE().getText() = " + name);
+
+ attributesInverted = true;
+ }
+
+ @Override
+ public void enterAttributeNormal(GnuLdParser.AttributeNormalContext ctx) {
+// System.out.println("ElfinfoGnuLdListener.enterAttributeNormal");
+
+ String name = ctx.NAME().getText();
+// System.out.println("ctx.ATTRIBUTE().getText() = " + name);
+ for (int i = 0; i < name.length(); i++) {
+ attribute = LdLoader.MemoryAttribute.valueOf(String.valueOf(Character.toUpperCase(name.charAt(i))));
+ attributes.add(attribute);
+ }
+ attributesInverted = false;
+ }
+}
diff --git a/cmake/elfinfo/src/main/java/io/trygvis/ld/ElfinfoGnuLdVisitor.java b/cmake/elfinfo/src/main/java/io/trygvis/ld/ElfinfoGnuLdVisitor.java
new file mode 100644
index 0000000..3062aad
--- /dev/null
+++ b/cmake/elfinfo/src/main/java/io/trygvis/ld/ElfinfoGnuLdVisitor.java
@@ -0,0 +1,75 @@
+package io.trygvis.ld;
+
+import io.trygvis.ld.antlr.GnuLdParser;
+import io.trygvis.ld.antlr.GnuLdParserBaseVisitor;
+import org.antlr.v4.runtime.tree.ParseTreeProperty;
+
+import java.math.BigInteger;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+public class ElfinfoGnuLdVisitor extends GnuLdParserBaseVisitor<ElfinfoGnuLdVisitor> {
+ public List<LdLoader.MemoryArea> memoryAreas = new ArrayList<>();
+
+ ParseTreeProperty<BigInteger> es = new ParseTreeProperty<>();
+
+ @Override
+ public ElfinfoGnuLdVisitor visitMemory_spec(GnuLdParser.Memory_specContext ctx) {
+ System.out.println("ElfinfoGnuLdBaseVisitor.visitMemory_spec");
+ visitChildren(ctx);
+
+ if (attributesInverted) {
+ // not quite sure how this is supposed to work yet
+ throw new RuntimeException("Inverted memory region attributes is not implemented yet.");
+ }
+
+ LdLoader.MemoryArea ma = new LdLoader.MemoryArea();
+ ma.name = ctx.NAME().getText();
+ ma.attributes = attributes;
+ ma.origin = ElfinfoExprVisitor.evaluate(ctx.origin_spec().mustbe_exp());
+ ma.length = ElfinfoExprVisitor.evaluate(ctx.length_spec().mustbe_exp());
+ System.out.println(ma);
+ memoryAreas.add(ma);
+ return this;
+ }
+
+ LdLoader.MemoryAttribute attribute;
+ boolean attributesInverted;
+ Set<LdLoader.MemoryAttribute> attributes;
+
+ @Override
+ public ElfinfoGnuLdVisitor visitAttributes_opt(GnuLdParser.Attributes_optContext ctx) {
+ System.out.println("ElfinfoGnuLdBaseVisitor.visitAttributes_opt");
+ attributes = new HashSet<>();
+ return visitChildren(ctx);
+ }
+
+ @Override
+ public ElfinfoGnuLdVisitor visitAttributeNormal(GnuLdParser.AttributeNormalContext ctx) {
+ System.out.println("ElfinfoGnuLdBaseVisitor.visitAttributeNormal");
+ String name = ctx.NAME().getText();
+ System.out.println("ctx.ATTRIBUTE().getText() = " + name);
+ for (int i = 0; i < name.length(); i++) {
+ attribute = LdLoader.MemoryAttribute.valueOf(String.valueOf(Character.toUpperCase(name.charAt(i))));
+ attributes.add(attribute);
+ }
+ attributesInverted = false;
+ return visitChildren(ctx);
+ }
+
+ @Override
+ public ElfinfoGnuLdVisitor visitAttributeInverted(GnuLdParser.AttributeInvertedContext ctx) {
+ System.out.println("ElfinfoGnuLdBaseVisitor.visitAttributeInverted");
+ if (!attributes.isEmpty()) {
+ throw new RuntimeException("Attributes for memory areas can only be attributesInverted (with '!') as the first character in a specification; foo(!rw), not foo(x!rw).");
+ }
+// String name = ctx.name().getText();
+ String name = ctx.NAME().getText();
+ System.out.println("ctx.ATTRIBUTE().getText() = " + name);
+
+ attributesInverted = true;
+ return visitChildren(ctx);
+ }
+}
diff --git a/cmake/elfinfo/src/main/java/io/trygvis/ld/LdLoader.java b/cmake/elfinfo/src/main/java/io/trygvis/ld/LdLoader.java
new file mode 100644
index 0000000..0b71720
--- /dev/null
+++ b/cmake/elfinfo/src/main/java/io/trygvis/ld/LdLoader.java
@@ -0,0 +1,121 @@
+package io.trygvis.ld;
+
+import io.trygvis.ld.antlr.GnuLdLexer;
+import io.trygvis.ld.antlr.GnuLdParser;
+import org.antlr.v4.runtime.ANTLRFileStream;
+import org.antlr.v4.runtime.BufferedTokenStream;
+import org.antlr.v4.runtime.ConsoleErrorListener;
+
+import java.io.File;
+import java.math.BigInteger;
+import java.util.List;
+import java.util.Set;
+
+public class LdLoader {
+
+ public final List<MemoryArea> memoryAreas;
+
+ public LdLoader(List<MemoryArea> memoryAreas) {
+ this.memoryAreas = memoryAreas;
+ }
+
+ public enum MemoryAttribute {
+ R, W, X, A, I,
+ }
+
+ public static class MemoryArea {
+ private final BigInteger MEGA = BigInteger.valueOf(1024 * 1024);
+ private final BigInteger KILO = BigInteger.valueOf(1024);
+
+ String name;
+ BigInteger origin;
+ BigInteger length;
+ Set<MemoryAttribute> attributes;
+
+ @Override
+ public String toString() {
+ return "MemoryArea{" +
+ "name='" + name + '\'' +
+ ", origin=" + origin +
+ ", length=" + length +
+ ", attributes='" + attributes + '\'' +
+ '}';
+ }
+
+ public String prettyOrigin() {
+ if (origin == null) {
+ return "";
+ }
+ String hex = origin.toString(16);
+
+ return "0x" + ("00000000").substring(0, 8 - hex.length()) + hex;
+ }
+
+ public String prettyLength() {
+ if (length == null) {
+ return "";
+ }
+
+ if (length.compareTo(MEGA) >= 0 && length.mod(MEGA).equals(BigInteger.ZERO)) {
+ return length.divide(MEGA) + "M";
+ }
+
+ if (length.compareTo(KILO) >= 0 && length.mod(KILO).equals(BigInteger.ZERO)) {
+ return length.divide(KILO) + "k";
+ }
+
+ return String.valueOf(length);
+ }
+
+ public String prettyAttributes() {
+ String s = "";
+ for (MemoryAttribute attribute : attributes) {
+ s += attribute.name().toLowerCase();
+ }
+ return s;
+ }
+ }
+
+ public static LdLoader load(File path) throws Exception {
+ GnuLdLexer lexer = new GnuLdLexer(new ANTLRFileStream(path.getAbsolutePath()));
+ BufferedTokenStream tokens = new BufferedTokenStream(lexer);
+ GnuLdParser parser = new GnuLdParser(tokens);
+ parser.setBuildParseTree(true);
+ parser.removeErrorListeners();
+ ConsoleErrorListener errorListener = new ConsoleErrorListener();
+ parser.addErrorListener(errorListener);
+ CollectingErrorListener collectingErrorListener = new CollectingErrorListener();
+ parser.addErrorListener(collectingErrorListener);
+
+ ElfinfoGnuLdListener listener = new ElfinfoGnuLdListener();
+ parser.addParseListener(listener);
+
+ GnuLdParser.FileContext file = parser.file();
+
+ if (parser.getNumberOfSyntaxErrors() > 0) {
+ throw new ParseErrorException(lexer, tokens, parser, collectingErrorListener.errors
+ );
+ }
+
+// ElfinfoGnuLdVisitor visitor = new ElfinfoGnuLdVisitor();
+// visitor.visit(file);
+
+// return new LdLoader(visitor.memoryAreas);
+ return new LdLoader(listener.memoryAreas);
+ }
+
+ public static class ParseErrorException extends Exception {
+ public final GnuLdLexer lexer;
+ public final BufferedTokenStream tokens;
+ public final GnuLdParser parser;
+ public final List<String> errors;
+
+ public ParseErrorException(GnuLdLexer lexer, BufferedTokenStream tokens, GnuLdParser parser, List<String> errors) {
+ this.lexer = lexer;
+ this.tokens = tokens;
+ this.parser = parser;
+ this.errors = errors;
+ }
+ }
+
+}
diff --git a/cmake/elfinfo/src/main/java/io/trygvis/ld/LdScript.java b/cmake/elfinfo/src/main/java/io/trygvis/ld/LdScript.java
deleted file mode 100644
index f3c93ac..0000000
--- a/cmake/elfinfo/src/main/java/io/trygvis/ld/LdScript.java
+++ /dev/null
@@ -1,498 +0,0 @@
-package io.trygvis.ld;
-
-import io.trygvis.ld.antlr.GnuLdLexer;
-import io.trygvis.ld.antlr.GnuLdParser;
-import io.trygvis.ld.antlr.GnuLdParserBaseVisitor;
-import org.antlr.v4.runtime.ANTLRFileStream;
-import org.antlr.v4.runtime.BufferedTokenStream;
-import org.antlr.v4.runtime.ConsoleErrorListener;
-import org.antlr.v4.runtime.tree.ParseTreeProperty;
-
-import java.io.File;
-import java.math.BigInteger;
-import java.util.ArrayList;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
-
-public class LdScript {
-
- public final List<MemoryArea> memoryAreas;
-
- public LdScript(List<MemoryArea> memoryAreas) {
- this.memoryAreas = memoryAreas;
- }
-
- public enum MemoryAttribute {
- R, W, X, A, I,
- }
-
- public static class MemoryArea {
- private final BigInteger MEGA = BigInteger.valueOf(1024 * 1024);
- private final BigInteger KILO = BigInteger.valueOf(1024);
-
- String name;
- BigInteger origin;
- BigInteger length;
- Set<MemoryAttribute> attributes;
-
- @Override
- public String toString() {
- return "MemoryArea{" +
- "name='" + name + '\'' +
- ", origin=" + origin +
- ", length=" + length +
- ", attributes='" + attributes + '\'' +
- '}';
- }
-
- public String prettyOrigin() {
- if (origin == null) {
- return "";
- }
- String hex = origin.toString(16);
-
- return "0x" + ("00000000").substring(0, 8 - hex.length()) + hex;
- }
-
- public String prettyLength() {
- if (length == null) {
- return "";
- }
-
- if (length.compareTo(MEGA) >= 0 && length.mod(MEGA).equals(BigInteger.ZERO)) {
- return length.divide(MEGA) + "M";
- }
-
- if (length.compareTo(KILO) >= 0 && length.mod(KILO).equals(BigInteger.ZERO)) {
- return length.divide(KILO) + "k";
- }
-
- return String.valueOf(length);
- }
-
- public String prettyAttributes() {
- String s = "";
- for (MemoryAttribute attribute : attributes) {
- s += attribute.name().toLowerCase();
- }
- return s;
- }
- }
-
- public static class Section {
- String name;
- }
-
- public static LdScript parse(File path) throws Exception {
- GnuLdLexer lexer = new GnuLdLexer(new ANTLRFileStream(path.getAbsolutePath()));
- BufferedTokenStream tokens = new BufferedTokenStream(lexer);
- GnuLdParser parser = new GnuLdParser(tokens);
- parser.setBuildParseTree(true);
- parser.removeErrorListeners();
- ConsoleErrorListener errorListener = new ConsoleErrorListener();
- parser.addErrorListener(errorListener);
- CollectingErrorListener collectingErrorListener = new CollectingErrorListener();
- parser.addErrorListener(collectingErrorListener);
-
- GnuLdParser.FileContext file = parser.file();
-
- if (parser.getNumberOfSyntaxErrors() > 0) {
- throw new ParseErrorException(lexer, tokens, parser, collectingErrorListener.errors
- );
- }
-
- ElfinfoGnuLdBaseVisitor visitor = new ElfinfoGnuLdBaseVisitor();
- visitor.visit(file);
-
- return new LdScript(visitor.memoryAreas);
- }
-
- private static class ElfinfoExprGnuLdBaseVisitor extends GnuLdParserBaseVisitor<ElfinfoExprGnuLdBaseVisitor> {
- private BigInteger value;
- private ParseTreeProperty<BigInteger> es = new ParseTreeProperty<>();
-
- public static BigInteger evaluate(GnuLdParser.Mustbe_expContext ctx) {
- ElfinfoExprGnuLdBaseVisitor v = new ElfinfoExprGnuLdBaseVisitor();
- v.visitMustbe_exp(ctx);
- return v.value();
- }
-
- @Override
- public ElfinfoExprGnuLdBaseVisitor visitExpLt(GnuLdParser.ExpLtContext ctx) {
- return visitChildren(ctx);
- }
-
- @Override
- public ElfinfoExprGnuLdBaseVisitor visitExpBlock(GnuLdParser.ExpBlockContext ctx) {
- return visitChildren(ctx);
-
- }
-
- @Override
- public ElfinfoExprGnuLdBaseVisitor visitExpAbsolute(GnuLdParser.ExpAbsoluteContext ctx) {
- return visitChildren(ctx);
-
- }
-
- @Override
- public ElfinfoExprGnuLdBaseVisitor visitExpLog2ceil(GnuLdParser.ExpLog2ceilContext ctx) {
- return visitChildren(ctx);
-
- }
-
- @Override
- public ElfinfoExprGnuLdBaseVisitor visitExpAddr(GnuLdParser.ExpAddrContext ctx) {
- return visitChildren(ctx);
-
- }
-
- @Override
- public ElfinfoExprGnuLdBaseVisitor visitExpDataSegmentEnd(GnuLdParser.ExpDataSegmentEndContext ctx) {
- return visitChildren(ctx);
- }
-
- @Override
- public ElfinfoExprGnuLdBaseVisitor visitExpSub(GnuLdParser.ExpSubContext ctx) {
- System.out.println("ElfinfoGnuLdBaseVisitor.visitExpSub");
-
- ElfinfoExprGnuLdBaseVisitor ret = visitChildren(ctx);
- BigInteger a = es.removeFrom(ctx.exp(0));
- BigInteger b = es.removeFrom(ctx.exp(1));
-
- BigInteger x = a.subtract(b);
-
- es.put(ctx, x);
- return ret;
- }
-
- @Override
- public ElfinfoExprGnuLdBaseVisitor visitExpDefined(GnuLdParser.ExpDefinedContext ctx) {
- return visitChildren(ctx);
- }
-
- @Override
- public ElfinfoExprGnuLdBaseVisitor visitExpMod(GnuLdParser.ExpModContext ctx) {
- return visitChildren(ctx);
- }
-
- @Override
- public ElfinfoExprGnuLdBaseVisitor visitExpInvert(GnuLdParser.ExpInvertContext ctx) {
- return visitChildren(ctx);
- }
-
- @Override
- public ElfinfoExprGnuLdBaseVisitor visitExpAlign(GnuLdParser.ExpAlignContext ctx) {
- return visitChildren(ctx);
- }
-
- @Override
- public ElfinfoExprGnuLdBaseVisitor visitExpMul(GnuLdParser.ExpMulContext ctx) {
- return visitChildren(ctx);
- }
-
- @Override
- public ElfinfoExprGnuLdBaseVisitor visitExpAnd(GnuLdParser.ExpAndContext ctx) {
- return visitChildren(ctx);
- }
-
- @Override
- public ElfinfoExprGnuLdBaseVisitor visitExpXor(GnuLdParser.ExpXorContext ctx) {
- return visitChildren(ctx);
- }
-
- @Override
- public ElfinfoExprGnuLdBaseVisitor visitExpParen(GnuLdParser.ExpParenContext ctx) {
- return visitChildren(ctx);
- }
-
- @Override
- public ElfinfoExprGnuLdBaseVisitor visitExpMinus(GnuLdParser.ExpMinusContext ctx) {
- return visitChildren(ctx);
- }
-
- @Override
- public ElfinfoExprGnuLdBaseVisitor visitExpDiv(GnuLdParser.ExpDivContext ctx) {
- return visitChildren(ctx);
- }
-
- @Override
- public ElfinfoExprGnuLdBaseVisitor visitExpGe(GnuLdParser.ExpGeContext ctx) {
- return visitChildren(ctx);
- }
-
- @Override
- public ElfinfoExprGnuLdBaseVisitor visitExpMin(GnuLdParser.ExpMinContext ctx) {
- return visitChildren(ctx);
- }
-
- @Override
- public ElfinfoExprGnuLdBaseVisitor visitExpAlignK(GnuLdParser.ExpAlignKContext ctx) {
- return visitChildren(ctx);
- }
-
- @Override
- public ElfinfoExprGnuLdBaseVisitor visitExpNegate(GnuLdParser.ExpNegateContext ctx) {
- return visitChildren(ctx);
- }
-
- @Override
- public ElfinfoExprGnuLdBaseVisitor visitExpName(GnuLdParser.ExpNameContext ctx) {
- return visitChildren(ctx);
- }
-
- @Override
- public ElfinfoExprGnuLdBaseVisitor visitExpOr(GnuLdParser.ExpOrContext ctx) {
- return visitChildren(ctx);
- }
-
- @Override
- public ElfinfoExprGnuLdBaseVisitor visitExpTrinary(GnuLdParser.ExpTrinaryContext ctx) {
- return visitChildren(ctx);
- }
-
- @Override
- public ElfinfoExprGnuLdBaseVisitor visitExpOror(GnuLdParser.ExpOrorContext ctx) {
- return visitChildren(ctx);
- }
-
- @Override
- public ElfinfoExprGnuLdBaseVisitor visitExpDataSegmentAlign(GnuLdParser.ExpDataSegmentAlignContext ctx) {
- return visitChildren(ctx);
- }
-
- @Override
- public ElfinfoExprGnuLdBaseVisitor visitExpLengthExp(GnuLdParser.ExpLengthExpContext ctx) {
- return visitChildren(ctx);
- }
-
- @Override
- public ElfinfoExprGnuLdBaseVisitor visitExpAdd(GnuLdParser.ExpAddContext ctx) {
- return visitChildren(ctx);
- }
-
- @Override
- public ElfinfoExprGnuLdBaseVisitor visitExpLoadaddr(GnuLdParser.ExpLoadaddrContext ctx) {
- return visitChildren(ctx);
- }
-
- @Override
- public ElfinfoExprGnuLdBaseVisitor visitExpGt(GnuLdParser.ExpGtContext ctx) {
- return visitChildren(ctx);
- }
-
- @Override
- public ElfinfoExprGnuLdBaseVisitor visitExpOrigin(GnuLdParser.ExpOriginContext ctx) {
- return visitChildren(ctx);
- }
-
- @Override
- public ElfinfoExprGnuLdBaseVisitor visitExpEq(GnuLdParser.ExpEqContext ctx) {
- return visitChildren(ctx);
- }
-
- @Override
- public ElfinfoExprGnuLdBaseVisitor visitExpMax(GnuLdParser.ExpMaxContext ctx) {
- return visitChildren(ctx);
- }
-
- @Override
- public ElfinfoExprGnuLdBaseVisitor visitExpLshift(GnuLdParser.ExpLshiftContext ctx) {
- return visitChildren(ctx);
- }
-
- @Override
- public ElfinfoExprGnuLdBaseVisitor visitExpSizeofHeaders(GnuLdParser.ExpSizeofHeadersContext ctx) {
- return visitChildren(ctx);
- }
-
- @Override
- public ElfinfoExprGnuLdBaseVisitor visitExpLe(GnuLdParser.ExpLeContext ctx) {
- return visitChildren(ctx);
- }
-
- @Override
- public ElfinfoExprGnuLdBaseVisitor visitExpNe(GnuLdParser.ExpNeContext ctx) {
- return visitChildren(ctx);
- }
-
- @Override
- public ElfinfoExprGnuLdBaseVisitor visitExpAndand(GnuLdParser.ExpAndandContext ctx) {
- return visitChildren(ctx);
- }
-
- @Override
- public ElfinfoExprGnuLdBaseVisitor visitExpRshift(GnuLdParser.ExpRshiftContext ctx) {
- return visitChildren(ctx);
- }
-
- @Override
- public ElfinfoExprGnuLdBaseVisitor visitExpNextParen(GnuLdParser.ExpNextParenContext ctx) {
- return visitChildren(ctx);
- }
-
- @Override
- public ElfinfoExprGnuLdBaseVisitor visitExpAlignof(GnuLdParser.ExpAlignofContext ctx) {
- return visitChildren(ctx);
- }
-
- @Override
- public ElfinfoExprGnuLdBaseVisitor visitExpSegmentStart(GnuLdParser.ExpSegmentStartContext ctx) {
- return visitChildren(ctx);
- }
-
- @Override
- public ElfinfoExprGnuLdBaseVisitor visitExpInt(GnuLdParser.ExpIntContext ctx) {
- System.out.println("ElfinfoGnuLdBaseVisitor.visitExpInt");
-
- String str = ctx.INT().getText().toLowerCase();
- int base = 10;
- if (str.startsWith("0x")) {
- base = 16;
- str = str.substring(2);
- }
-
- int factor = 1;
- if (str.endsWith("k")) {
- factor = 1024;
- str = str.substring(0, str.length() - 1);
- } else if (str.endsWith("k")) {
- factor = 1024 * 1024;
- str = str.substring(0, str.length() - 1);
- }
-
- BigInteger i = new BigInteger(str, base);
- if (factor > 1) {
- i = i.multiply(BigInteger.valueOf(factor));
- }
- System.out.println("ctx = " + ctx);
- System.out.println("i = " + i);
- es.put(ctx, i);
-
- return this;
- }
-
- @Override
- public ElfinfoExprGnuLdBaseVisitor visitExpConstant(GnuLdParser.ExpConstantContext ctx) {
- return visitChildren(ctx);
- }
-
- @Override
- public ElfinfoExprGnuLdBaseVisitor visitExpPlus(GnuLdParser.ExpPlusContext ctx) {
- return visitChildren(ctx);
- }
-
- @Override
- public ElfinfoExprGnuLdBaseVisitor visitExpSizeof(GnuLdParser.ExpSizeofContext ctx) {
- return visitChildren(ctx);
- }
-
- @Override
- public ElfinfoExprGnuLdBaseVisitor visitExpDataSegmentRelRoEnd(GnuLdParser.ExpDataSegmentRelRoEndContext ctx) {
- return visitChildren(ctx);
- }
-
- @Override
- public ElfinfoExprGnuLdBaseVisitor visitExpAssert(GnuLdParser.ExpAssertContext ctx) {
- return visitChildren(ctx);
- }
-
- @Override
- public ElfinfoExprGnuLdBaseVisitor visitMustbe_exp(GnuLdParser.Mustbe_expContext ctx) {
- ElfinfoExprGnuLdBaseVisitor ret = visitChildren(ctx);
-
- value = es.removeFrom(ctx.exp());
- es.put(ctx, value);
-
- return ret;
- }
-
- public BigInteger value() {
- if (value != null) {
- return value;
- }
-
- throw new RuntimeException("Something bad happened, probably an unevaluated expression part.");
- }
- }
-
- public static class ElfinfoGnuLdBaseVisitor extends GnuLdParserBaseVisitor<ElfinfoGnuLdBaseVisitor> {
- public List<MemoryArea> memoryAreas = new ArrayList<>();
-
- public List<Section> sections = new ArrayList<>();
-
- ParseTreeProperty<BigInteger> es = new ParseTreeProperty<>();
-
- @Override
- public ElfinfoGnuLdBaseVisitor visitMemory_spec(GnuLdParser.Memory_specContext ctx) {
- System.out.println("ElfinfoGnuLdBaseVisitor.visitMemory_spec");
- visitChildren(ctx);
-
- if (attributesInverted) {
- // not quite sure how this is supposed to work yet
- throw new RuntimeException("Inverted memory region attributes is not implemented yet.");
- }
-
- MemoryArea ma = new MemoryArea();
- ma.name = ctx.NAME().getText();
- ma.attributes = attributes;
- ma.origin = ElfinfoExprGnuLdBaseVisitor.evaluate(ctx.origin_spec().mustbe_exp());
- ma.length = ElfinfoExprGnuLdBaseVisitor.evaluate(ctx.length_spec().mustbe_exp());
- System.out.println(ma);
- memoryAreas.add(ma);
- return this;
- }
-
- MemoryAttribute attribute;
- boolean attributesInverted;
- Set<MemoryAttribute> attributes;
-
- @Override
- public ElfinfoGnuLdBaseVisitor visitAttributes_opt(GnuLdParser.Attributes_optContext ctx) {
- System.out.println("ElfinfoGnuLdBaseVisitor.visitAttributes_opt");
- attributes = new HashSet<>();
- return visitChildren(ctx);
- }
-
- @Override
- public ElfinfoGnuLdBaseVisitor visitAttributeNormal(GnuLdParser.AttributeNormalContext ctx) {
- System.out.println("ElfinfoGnuLdBaseVisitor.visitAttributeNormal");
- String name = ctx.NAME().getText();
- System.out.println("ctx.ATTRIBUTE().getText() = " + name);
- for (int i = 0; i < name.length(); i++) {
- attribute = MemoryAttribute.valueOf(String.valueOf(Character.toUpperCase(name.charAt(i))));
- attributes.add(attribute);
- }
- attributesInverted = false;
- return visitChildren(ctx);
- }
-
- @Override
- public ElfinfoGnuLdBaseVisitor visitAttributeInverted(GnuLdParser.AttributeInvertedContext ctx) {
- System.out.println("ElfinfoGnuLdBaseVisitor.visitAttributeInverted");
- if (!attributes.isEmpty()) {
- throw new RuntimeException("Attributes for memory areas can only be attributesInverted (with '!') as the first character in a specification; foo(!rw), not foo(x!rw).");
- }
-// String name = ctx.name().getText();
- String name = ctx.NAME().getText();
- System.out.println("ctx.ATTRIBUTE().getText() = " + name);
-
- attributesInverted = true;
- return visitChildren(ctx);
- }
- }
-
- public static class ParseErrorException extends Exception {
- public final GnuLdLexer lexer;
- public final BufferedTokenStream tokens;
- public final GnuLdParser parser;
- public final List<String> errors;
-
- public ParseErrorException(GnuLdLexer lexer, BufferedTokenStream tokens, GnuLdParser parser, List<String> errors) {
- this.lexer = lexer;
- this.tokens = tokens;
- this.parser = parser;
- this.errors = errors;
- }
- }
-}
diff --git a/cmake/elfinfo/src/main/test/io/trygvis/ld/FullScriptsTest.java b/cmake/elfinfo/src/main/test/io/trygvis/ld/FullScriptsTest.java
index 1452f1f..0c5a30c 100644
--- a/cmake/elfinfo/src/main/test/io/trygvis/ld/FullScriptsTest.java
+++ b/cmake/elfinfo/src/main/test/io/trygvis/ld/FullScriptsTest.java
@@ -19,13 +19,13 @@ public class FullScriptsTest {
private void fullScript2(String fileName) throws Exception {
try {
- LdScript script = LdScript.parse(new File(fileName));
+ LdLoader script = LdLoader.load(new File(fileName));
System.out.println("--------------------------------------------------------");
- for (LdScript.MemoryArea area : script.memoryAreas) {
+ for (LdLoader.MemoryArea area : script.memoryAreas) {
System.out.println(" " + area.name + "(" + area.prettyAttributes() + ") : ORIGIN = " + area.prettyOrigin() + ", LENGTH=" + area.prettyLength());
}
- } catch (LdScript.ParseErrorException e) {
+ } catch (LdLoader.ParseErrorException e) {
Test1Test.showTokens(e.tokens, e.parser);
System.out.println("Got " + e.errors.size() + " errors:");
e.errors.forEach(System.out::println);