* net/loveruby/cflat/compiler/Compiler.java: implement -shared.

* net/loveruby/cflat/compiler/Options.java: new option -shared.
* net/loveruby/cflat/compiler/CodeGeneratorOptions.java: ditto.
* net/loveruby/cflat/compiler/SourceFile.java: new method #linkedFileName.


git-svn-id: file:///Users/aamine/c/gitwork/public/cbc/trunk@4065 1b9489fe-b721-0410-924e-b54b9192deb8
This commit is contained in:
Minero Aoki 2008-09-27 22:07:04 +00:00
parent 94db94b1c4
commit 1d35dd1665
6 changed files with 98 additions and 23 deletions

View File

@ -1,3 +1,14 @@
Sun Sep 28 07:06:37 2008 Minero Aoki <aamine@loveruby.net>
* net/loveruby/cflat/compiler/Compiler.java: implement -shared.
* net/loveruby/cflat/compiler/Options.java: new option -shared.
* net/loveruby/cflat/compiler/CodeGeneratorOptions.java: ditto.
* net/loveruby/cflat/compiler/SourceFile.java: new method
#linkedFileName.
Sun Sep 28 05:07:30 2008 Minero Aoki <aamine@loveruby.net> Sun Sep 28 05:07:30 2008 Minero Aoki <aamine@loveruby.net>
* implement PIC generation. * implement PIC generation.

4
ToDo
View File

@ -7,10 +7,9 @@
* Label -> Symbol * Label -> Symbol
* all Symbols should be got from objects * all Symbols should be got from objects
* named/unnamed Label * named/unnamed Label
* -shared
* bin/cbc: resolve symbolic link * bin/cbc: resolve symbolic link
* remove all FIXME * remove all FIXME
? -fPIE, -pie * -fPIE, -pie
== Done == Done
@ -252,3 +251,4 @@
- -O (jump elimination) - -O (jump elimination)
- useless label elimination - useless label elimination
- -fPIC (PIC generation) - -fPIC (PIC generation)
- -shared

View File

@ -5,11 +5,13 @@ import java.util.*;
class CodeGeneratorOptions { class CodeGeneratorOptions {
protected int optimizeLevel; protected int optimizeLevel;
protected boolean generatePIC; protected boolean generatePIC;
protected boolean generatePIE;
protected boolean verboseAsm; protected boolean verboseAsm;
public CodeGeneratorOptions() { public CodeGeneratorOptions() {
optimizeLevel = 0; optimizeLevel = 0;
generatePIC = false; generatePIC = false;
generatePIE = false;
verboseAsm = false; verboseAsm = false;
} }
@ -38,6 +40,10 @@ class CodeGeneratorOptions {
return verboseAsm; return verboseAsm;
} }
public boolean isPositionIndependent() {
return generatePIC || generatePIE;
}
public void generatePIC() { public void generatePIC() {
this.generatePIC = true; this.generatePIC = true;
} }
@ -45,4 +51,12 @@ class CodeGeneratorOptions {
public boolean isPICRequired() { public boolean isPICRequired() {
return generatePIC; return generatePIC;
} }
public void generatePIE() {
this.generatePIE = true;
}
public boolean isPIERequired() {
return generatePIE;
}
} }

View File

@ -59,7 +59,12 @@ public class Compiler {
compileFile(src, opts); compileFile(src, opts);
} }
if (! opts.isLinkRequired()) System.exit(0); if (! opts.isLinkRequired()) System.exit(0);
generateExecutable(opts); if (! opts.isGeneratingSharedLibrary()) {
generateExecutable(opts);
}
else {
generateSharedLibrary(opts);
}
System.exit(0); System.exit(0);
} }
catch (CompileException ex) { catch (CompileException ex) {
@ -201,15 +206,25 @@ public class Compiler {
invoke(cmd, opts.isVerboseMode()); invoke(cmd, opts.isVerboseMode());
} }
static final protected String DYNAMIC_LINKER = "/lib/ld-linux.so.2";
static final protected String C_RUNTIME_INIT = "/usr/lib/crti.o";
static final protected String C_RUNTIME_START = "/usr/lib/crt1.o";
static final protected String C_RUNTIME_START_PIE = "/usr/lib/Scrt1.o";
static final protected String C_RUNTIME_FINI = "/usr/lib/crtn.o";
protected void generateExecutable(Options opts) throws IPCException { protected void generateExecutable(Options opts) throws IPCException {
List cmd = new ArrayList(); List cmd = new ArrayList();
cmd.add("ld"); cmd.add("ld");
// FIXME: -dynamic-linker required only on dynamic linking
cmd.add("-dynamic-linker"); cmd.add("-dynamic-linker");
cmd.add("/lib/ld-linux.so.2"); cmd.add(DYNAMIC_LINKER);
if (opts.isGeneratingPIE()) {
cmd.add("-pie");
}
if (! opts.noStartFiles()) { if (! opts.noStartFiles()) {
cmd.add("/usr/lib/crt1.o"); cmd.add(opts.isGeneratingPIE()
cmd.add("/usr/lib/crti.o"); ? C_RUNTIME_START_PIE
: C_RUNTIME_START);
cmd.add(C_RUNTIME_INIT);
} }
cmd.addAll(opts.ldArgs()); cmd.addAll(opts.ldArgs());
if (! opts.noDefaultLibs()) { if (! opts.noDefaultLibs()) {
@ -217,13 +232,33 @@ public class Compiler {
cmd.add("-lcbc"); cmd.add("-lcbc");
} }
if (! opts.noStartFiles()) { if (! opts.noStartFiles()) {
cmd.add("/usr/lib/crtn.o"); cmd.add(C_RUNTIME_FINI);
} }
cmd.add("-o"); cmd.add("-o");
cmd.add(opts.exeFileName()); cmd.add(opts.exeFileName());
invoke(cmd, opts.isVerboseMode()); invoke(cmd, opts.isVerboseMode());
} }
protected void generateSharedLibrary(Options opts) throws IPCException {
List cmd = new ArrayList();
cmd.add("ld");
cmd.add("-shared");
if (! opts.noStartFiles()) {
cmd.add(C_RUNTIME_INIT);
}
cmd.addAll(opts.ldArgs());
if (! opts.noDefaultLibs()) {
cmd.add("-lc");
cmd.add("-lcbc");
}
if (! opts.noStartFiles()) {
cmd.add(C_RUNTIME_FINI);
}
cmd.add("-o");
cmd.add(opts.soFileName());
invoke(cmd, opts.isVerboseMode());
}
protected void invoke(List cmdArgs, boolean debug) throws IPCException { protected void invoke(List cmdArgs, boolean debug) throws IPCException {
if (debug) { if (debug) {
dumpCommand(cmdArgs.iterator()); dumpCommand(cmdArgs.iterator());

View File

@ -15,6 +15,8 @@ class Options {
protected boolean debugParser; protected boolean debugParser;
protected CodeGeneratorOptions genOptions; protected CodeGeneratorOptions genOptions;
protected List asOptions; // List<String> protected List asOptions; // List<String>
protected boolean generatingSharedLibrary;
protected boolean generatingPIE;
protected List ldArgs; // List<LdArg> protected List ldArgs; // List<LdArg>
protected boolean noStartFiles = false; protected boolean noStartFiles = false;
protected boolean noDefaultLibs = false; protected boolean noDefaultLibs = false;
@ -24,6 +26,8 @@ class Options {
this.loader = loader; this.loader = loader;
this.genOptions = new CodeGeneratorOptions(); this.genOptions = new CodeGeneratorOptions();
this.asOptions = new ArrayList(); this.asOptions = new ArrayList();
this.generatingSharedLibrary = false;
this.generatingPIE = false;
this.ldArgs = new ArrayList(); this.ldArgs = new ArrayList();
} }
@ -71,11 +75,18 @@ class Options {
} }
public String exeFileName() { public String exeFileName() {
if (outputFileName != null) return outputFileName; return getOutputFileName("");
}
public String soFileName() {
return getOutputFileName(".so");
}
protected String getOutputFileName(String newExt) {
List srcs = sourceFiles(); List srcs = sourceFiles();
if (srcs.size() == 1) { if (srcs.size() == 1) {
SourceFile src = (SourceFile)srcs.get(0); SourceFile src = (SourceFile)srcs.get(0);
return src.exeFileName(this); return src.linkedFileName(this, newExt);
} }
else { else {
return "a.out"; return "a.out";
@ -123,6 +134,10 @@ class Options {
return this.asOptions; return this.asOptions;
} }
public boolean isGeneratingSharedLibrary() {
return this.generatingSharedLibrary;
}
// List<ldArg> // List<ldArg>
public List ldArgs() { public List ldArgs() {
return this.ldArgs; return this.ldArgs;
@ -171,15 +186,14 @@ class Options {
else if (arg.startsWith("-o")) { else if (arg.startsWith("-o")) {
outputFileName = getOptArg(arg, args); outputFileName = getOptArg(arg, args);
} }
else if (arg.equals("-fpic")) { else if (arg.equals("-fpic")
|| arg.equals("-fPIC")) {
genOptions.generatePIC(); genOptions.generatePIC();
} }
else if (arg.equals("-fPIC")) { else if (arg.equals("-fpie")
genOptions.generatePIC(); || arg.equals("-fPIE")) {
genOptions.generatePIE();
} }
// FIXME: PIE
//else if (arg.equals("-fpie"))
//else if (arg.equals("-fPIE"))
else if (arg.startsWith("-O")) { else if (arg.startsWith("-O")) {
String type = arg.substring(2); String type = arg.substring(2);
if (! type.matches("^([0123s]|)$")) { if (! type.matches("^([0123s]|)$")) {
@ -200,10 +214,11 @@ class Options {
ldArgs.add(new LdOption(arg)); ldArgs.add(new LdOption(arg));
} }
else if (arg.equals("-shared")) { else if (arg.equals("-shared")) {
// FIXME: ?? any more work is required?? generatingSharedLibrary = true;
ldArgs.add(new LdOption(arg)); }
else if (arg.equals("-pie")) {
generatingPIE = true;
} }
//else if (arg.equals("-pie"))
else if (arg.startsWith("-L")) { else if (arg.startsWith("-L")) {
ldArgs.add(new LdOption("-L" + getOptArg(arg, args))); ldArgs.add(new LdOption("-L" + getOptArg(arg, args)));
} }
@ -338,7 +353,7 @@ class Options {
out.println(" -Os Equivalent to -O."); out.println(" -Os Equivalent to -O.");
out.println(" -O0 Disables optimization (default)."); out.println(" -O0 Disables optimization (default).");
out.println(" -fPIC Generates PIC assembly."); out.println(" -fPIC Generates PIC assembly.");
//out.println(" -fPIE Generates PIE assembly."); out.println(" -fPIE Generates PIE assembly.");
out.println(" --verbose-asm Generate assembly with verbose comments."); out.println(" --verbose-asm Generate assembly with verbose comments.");
out.println(""); out.println("");
out.println("Assembler Options:"); out.println("Assembler Options:");
@ -350,7 +365,7 @@ class Options {
out.println(" -L PATH Adds PATH as library directory."); out.println(" -L PATH Adds PATH as library directory.");
out.println(" -shared Generates shared library rather than executable."); out.println(" -shared Generates shared library rather than executable.");
out.println(" -static Linkes only with static libraries."); out.println(" -static Linkes only with static libraries.");
//out.println(" -pie Generates PIE."); out.println(" -pie Generates PIE.");
out.println(" -nostartfiles Do not link startup files."); out.println(" -nostartfiles Do not link startup files.");
out.println(" -nodefaultlibs Do not link default libraries."); out.println(" -nodefaultlibs Do not link default libraries.");
out.println(" -nostdlib Enables -nostartfiles and -nodefaultlibs."); out.println(" -nostdlib Enables -nostartfiles and -nodefaultlibs.");

View File

@ -59,8 +59,8 @@ class SourceFile implements LdArg {
return or(opts.outputFileNameFor("-c"), replaceExt(".o")); return or(opts.outputFileNameFor("-c"), replaceExt(".o"));
} }
public String exeFileName(Options opts) { public String linkedFileName(Options opts, String newExt) {
return or(opts.outputFileName, replaceExt("")); return or(opts.outputFileName, replaceExt(newExt));
} }
protected String or(String x, String y) { protected String or(String x, String y) {