Changeset 221
- Timestamp:
- 07/30/06 22:18:08 (2 years ago)
- Files:
-
- trunk/ddl/Linker.d (modified) (2 diffs)
- trunk/ddl/Mangle.d (modified) (1 diff)
- trunk/ddl/elf/ELFLibrary.d (modified) (3 diffs)
- trunk/ddl/elf/ELFModule.d (modified) (4 diffs)
- trunk/ddl/omf/OMFModule.d (modified) (4 diffs)
- trunk/test/linktest1.d (modified) (3 diffs)
- trunk/test/testmodule.d (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/ddl/Linker.d
r219 r221 208 208 } 209 209 } 210 211 debug debugLog(mod.toString()); 212 210 213 mod.resolveFixups(); 211 214 mod.isLinking = false; … … 214 217 throw new LinkModuleException(mod); 215 218 } 216 217 debug debugLog(mod.toString()); 218 219 219 220 // dig up the ModuleInfo (if applicable) and initalize it! 220 221 foreach(sym; mod.getSymbols){ trunk/ddl/Mangle.d
r195 r221 36 36 37 37 import meta.conv; 38 import meta.strhacks;39 38 40 39 import mango.convert.Integer; trunk/ddl/elf/ELFLibrary.d
r176 r221 34 34 private import ddl.ExportSymbol; 35 35 private import ddl.Attributes; 36 private import ddl.Utils; 37 private import ddl.FileBuffer; 38 36 39 private import ddl.elf.ELFHeaders; 37 40 private import ddl.elf.ELFModule; 38 41 private import ddl.elf.ELFPrinter; 42 43 private import mango.io.Exception; 44 private import mango.io.model.IBuffer; 45 private import mango.io.model.IConduit; 39 46 40 47 /** … … 44 51 */ 45 52 class ELFLibrary : DynamicLibrary{ 46 ELFModule[] modules; 47 ExportSymbol[char[]] dictionary; 48 Attributes attributes; 53 DynamicModule[] modules; 54 DynamicModule[char[]] crossReference; // modules by symbol name 55 ExportSymbolPtr[char[]] dictionary; // symbols by symbol name 56 Attributes attributes; 57 58 public this(){ 59 attributes["elf.filename"] = "<unknown>"; 60 } 61 62 public this(FileBuffer file){ 63 attributes["elf.filename"] = file.getPath.toString(); 64 load(file); 65 } 66 67 public char[] getType(){ 68 return "OMF"; 69 } 70 71 public Attributes getAttributes(){ 72 return attributes; 73 } 49 74 50 75 package void setAttributes(Attributes other){ … … 54 79 package void setAttribute(char[] key,char[] value){ 55 80 this.attributes[key] = value; 56 } 57 58 /** 59 Empty constructor for ELFLibrary. 60 */ 61 public this(){ 62 } 63 64 /** 65 Returns a list of the library's export symbols as an 66 ExportSymbol[]. 67 */ 68 public ExportSymbol[] getExports(){ 69 return dictionary.values; 70 } 71 72 /** 73 Returns the ExportSymbol with the supplied name. 74 75 Returns: The ExportSymbol with the provided the name 76 Params: name = the name of the ExportSymbol that should be 77 returned 78 */ 79 public ExportSymbol getExport(char[] name){ 80 if(!(name in dictionary)){ 81 return ExportSymbol.NONE; 82 } 83 return dictionary[name]; 84 } 85 86 /** 87 Returns a list of the library's dynamic modules as a 88 DynamicModule[]. 89 */ 90 public DynamicModule[] getModules(){ 91 return modules; 92 } 93 94 /** 95 Adds an ELFModule to the library. 96 97 Params: mod = the module to be added 98 */ 99 package void addModule(ELFModule mod){ 100 this.modules ~= mod; 101 foreach(ExportSymbol exp; mod.getExports()){ 102 dictionary[exp.name] = exp; 103 } 104 } 105 106 /** 107 Returns the type of the library, in this case the static string 108 "ELFLibrary". 109 */ 110 public char[] getType(){ 111 return "ELFLibrary"; 112 } 113 114 /** 115 Returns an associated array of attributes. 116 */ 117 public Attributes getAttributes(){ 118 return attributes; 119 } 120 121 /** 122 Returns the DynamicModule associated with the supplied export 123 name. 124 125 Returns: A DynamicModule holding the symbol provided by the 126 supplied parameter. 127 Params: name = the name of the symbol 128 */ 129 public DynamicModule getModuleForExport(char[] name){ 130 // TODO: Implement 131 assert(false); 132 return null; 133 } 134 135 /** 136 Currently only returns null in this class. 137 */ 138 public ubyte[] getResource(char[] name){ 139 // TODO: Implement 140 return null; 141 } 142 81 } 82 83 public ExportSymbolPtr getSymbol(char[] name){ 84 ExportSymbolPtr* sym = name in dictionary; 85 if(sym) return *sym; 86 else return &ExportSymbol.NONE; 87 } 88 89 public DynamicModule[] getModules(){ 90 return this.modules; 91 } 92 93 public DynamicModule getModuleForSymbol(char[] name){ 94 debug debugLog("[OMF] looking for " ~ name); 95 DynamicModule* mod = name in crossReference; 96 debug debugLog("[OMF] Result: %0.8X",mod); 97 if(mod) return *mod; 98 return null; 99 } 100 101 public ubyte[] getResource(char[] name){ 102 return (ubyte[]).init; 103 } 104 105 package void addModule(ELFModule mod){ 106 this.modules ~= mod; 107 auto symbols = mod.getSymbols(); 108 for(uint i=0; i<symbols.length; i++){ 109 ExportSymbolPtr exp = &(symbols[i]); 110 if(exp.name in crossReference){ 111 switch(exp.type){ 112 case SymbolType.Weak: // replace extern only 113 if(dictionary[exp.name].type == SymbolType.Extern){ 114 crossReference[exp.name] = mod; 115 dictionary[exp.name] = exp; 116 } 117 break; 118 case SymbolType.Strong: // always overwrite 119 crossReference[exp.name] = mod; 120 dictionary[exp.name] = exp; 121 break; 122 default: 123 // do nothing 124 } 125 } 126 else{ 127 crossReference[exp.name] = mod; 128 dictionary[exp.name] = exp; 129 } 130 } 131 } 132 133 protected void load(IBuffer data){ 134 //TODO 135 } 143 136 } trunk/ddl/elf/ELFModule.d
r220 r221 36 36 private import ddl.Attributes; 37 37 38 39 38 private import ddl.elf.ELFBinary; 39 private import ddl.elf.ELFReader; 40 private import ddl.elf.ELFPrinter; 40 41 41 42 /** … … 69 70 this(FileBuffer buffer){ 70 71 resolved = false; 71 loadBinary(new DDLReader(buffer));72 loadBinary(new ELFReader(buffer)); 72 73 } 73 74 74 this( DDLReader reader){75 this(ELFReader reader){ 75 76 resolved = false; 76 77 loadBinary(reader); … … 111 112 } 112 113 113 protected void loadBinary( DDLReader reader){114 if(debug) else{115 ELFBinary binary;116 }114 protected void loadBinary(ELFReader reader){ 115 // debug() else{ 116 // ELFBinary binary; 117 // } 117 118 118 119 //TODO: analyze … … 123 124 ExtSprintClass sprint = new ExtSprintClass(1024); 124 125 125 if(debug){126 debug{ 126 127 result = "ELF Binary Data: \n" ~ binary.toString(); 127 128 } trunk/ddl/omf/OMFModule.d
r220 r221 110 110 //NOTE: namely, this includes __except_list and __nullext, which point to the start 111 111 // of their respective segments 112 * destAddress = fixupValue;112 *cast(uint*)destAddress = fixupValue; 113 113 } 114 114 else if(!isSegmentRelative){ 115 uint value = fixupValue - cast(uint)dest - 4; // relative fixup, offset by width of field 116 *destAddress = fixupValue; 115 *cast(uint*)destAddress = fixupValue - cast(uint)destAddress - 4; // relative fixup, offset by width of field 117 116 } 118 117 else{ // self relative 119 * destAddress += fixupValue;118 *cast(uint*)destAddress += fixupValue; 120 119 } 121 120 } … … 380 379 newFix.isSegmentRelative = fix.isSegmentRelative; 381 380 newFix.isExternStyleFixup = fix.isExternStyleFixup; 381 newFix.targetIndex = fix.targetIndex; 382 382 383 383 if(fix.destNameIndex > 0){ … … 423 423 char[] rel = isSegmentRelative ? "segmentRelative" : "selfRelative"; 424 424 char[] ext = isExternStyleFixup ? "externStyle" : "segmentStyle"; 425 void* targetAddress = &(segmentImages[destSegmentIndex].data[destOffset]);426 425 427 result ~= sprint(" %d: %0.8X %s %s",idx, targetAddress,rel,ext);426 result ~= sprint(" %d: %0.8X %s %s",idx,destAddress,rel,ext); 428 427 429 428 if(isExternStyleFixup){ … … 439 438 result ~= "DATA: \n"; 440 439 foreach(idx,segdef; segmentImages){ 440 char[] buf2 = ""; 441 441 result ~= sprint("segment %d data (%d bytes): ",idx,segdef.data.length); 442 442 foreach(idx,b; cast(ubyte[])(segdef.data)){ 443 if(idx % 4 == 0) result ~= sprint("\n\t [%0.8X] ",segdef.data.ptr+idx); 444 if(b < 127 && b > 32) result ~= sprint(" %c ",cast(char)b); 445 else result ~= sprint("%0.2X ",b); 443 if(idx % 16 == 0){ 444 result ~= sprint(" | %s\n\t [%0.8X] ",buf2,segdef.data.ptr+idx); 445 buf2 = ""; 446 } 447 result ~= sprint("%0.2X ",b); 448 if(b >= 32 && b <= 126){ 449 buf2 ~= cast(char)b ~ " "; 450 } 451 else{ 452 buf2 ~= ". "; 453 } 446 454 } 447 455 result ~= "\n"; trunk/test/linktest1.d
r219 r221 28 28 import ddl.all; 29 29 30 import std.stdio; 31 30 32 import mango.io.Stdout; 31 33 … … 42 44 private import test.testclassinterface; 43 45 44 uint add(uint a,uint b){45 asm{46 nop;47 nop;48 nop;49 }50 return 0;51 }52 53 46 void main(){ 54 47 Stdout.println("Starting."); … … 62 55 auto testLibrary = linker.loadAndLink("test\\testmodule.obj"); 63 56 auto addFun = testLibrary.getDExport!(uint function(uint,uint),"test.testmodule.add")(); 64 auto helloWorld = testLibrary.getDExport!(void function(),"test.testmodule.helloWorld")();65 66 uint* ptr = cast(uint*)(testLibrary.getDExport!(uint function(uint,uint),"test.testmodule.add")())+2;67 Stdout.println("addFun: [%0.8X] %0.8X %0.8X %0.8X %0.8X %0.8X %0.8X",ptr,*ptr,*(ptr+1),*(ptr+2),*(ptr+3),*(ptr+4),*(ptr+5));68 69 ptr = cast(uint*)(&add);70 Stdout.println("this add: [%0.8X] %0.8X %0.8X %0.8X %0.8X %0.8X %0.8X",ptr,*ptr,*(ptr+1),*(ptr+2),*(ptr+3),*(ptr+4),*(ptr+5));71 57 72 58 Stdout.println("add: 42+69 = %d",addFun(42,69)); 73 return; 59 Stdout.println("printf: %0.8X test printf: %0.8X",&printf,*cast(void**)testLibrary.getSymbol("_D4test10testmodule6foobarPv").address); 60 61 auto verify = testLibrary.getDExport!(bool function(void*),"test.testmodule.verifyPrintf")(); 62 char[] result = verify(&printf) ? "true" : "false"; 63 Stdout.println("verifyPrintf: %s",result); 64 65 auto getPrintf = testLibrary.getDExport!(void* function(),"test.testmodule.getPrintf")(); 66 Stdout.println("printf: %0.8X getPrintf: %0.8X",&printf,getPrintf()); 67 68 void* testString = testLibrary.getSymbol("_D4test10testmodule10testStringAa").address; 69 Stdout.println("testString: %0.8X [%0.8X %0.8X]",testString,*cast(uint*)testString,*cast(uint*)(testString+4)); 70 71 auto getTestString = testLibrary.getDExport!(void* function(),"test.testmodule.getTestString")(); 72 Stdout.println("getTestString: %0.8X",getTestString()); 73 Stdout.println(*cast(char[]*)getTestString()); 74 75 auto helloWorld = testLibrary.getDExport!(void function(),"test.testmodule.helloWorld")(); 74 76 helloWorld(); 75 77 78 auto getWritefln = testLibrary.getDExport!(void* function(),"test.testmodule.getTestString")(); 79 Stdout.println("getWritefln: %0.8X (%0.8X)",getWritefln,&writefln); 80 81 auto helloWorld2 = testLibrary.getDExport!(void function(),"test.testmodule.helloWorld2")(); 82 helloWorld2(); 83 84 auto classTest = testLibrary.getDExport!(void function(),"test.testmodule.classTest")(); 85 86 classTest(); 87 76 88 // Create a class using two different constructors. We need to specify 77 89 // the interface we'll be using (in this case, ITestClass). trunk/test/testmodule.d
r219 r221 29 29 import test.testclassinterface; 30 30 31 void* foobar = &printf; 31 32 33 public void* getPrintf(){ 34 return &printf; 35 } 32 36 class TestClass : ITestClass { 33 37 uint a; … … 70 74 } 71 75 76 public bool verifyPrintf(void* externalPrintf){ 77 return &printf == externalPrintf; 78 } 79 80 char[] testString = "Hello DDL World!\n"; 81 82 void* getTestString(){ 83 return &testString; 84 } 85 72 86 public void helloWorld(){ 87 printf("hello world\n"); 88 } 89 90 void* getWritefln(){ 91 return &writefln; 92 } 93 94 public void helloWorld2(){ 95 writefln("hello world2\n"); 96 } 97 98 public void classTest(){ 73 99 new TestClass(); 74 printf("Hello DDL World!\n");75 100 } 76 101
