Changeset 221

Show
Ignore:
Timestamp:
07/30/06 22:18:08 (2 years ago)
Author:
pragma
Message:

linktest1 works under this revision.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/ddl/Linker.d

    r219 r221  
    208208            } 
    209209        } 
     210 
     211        debug debugLog(mod.toString()); 
     212                 
    210213        mod.resolveFixups(); 
    211214        mod.isLinking = false; 
     
    214217            throw new LinkModuleException(mod); 
    215218        } 
    216  
    217         debug debugLog(mod.toString()); 
    218                          
     219                     
    219220        // dig up the ModuleInfo (if applicable) and initalize it! 
    220221        foreach(sym; mod.getSymbols){ 
  • trunk/ddl/Mangle.d

    r195 r221  
    3636 
    3737import meta.conv; 
    38 import meta.strhacks; 
    3938 
    4039import mango.convert.Integer; 
  • trunk/ddl/elf/ELFLibrary.d

    r176 r221  
    3434private import ddl.ExportSymbol; 
    3535private import ddl.Attributes; 
     36private import ddl.Utils; 
     37private import ddl.FileBuffer; 
     38 
    3639private import ddl.elf.ELFHeaders; 
    3740private import ddl.elf.ELFModule; 
    3841private import ddl.elf.ELFPrinter; 
     42 
     43private import mango.io.Exception; 
     44private import mango.io.model.IBuffer; 
     45private import mango.io.model.IConduit; 
    3946 
    4047/** 
     
    4451*/ 
    4552class 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    } 
    4974     
    5075    package void setAttributes(Attributes other){ 
     
    5479    package void setAttribute(char[] key,char[] value){ 
    5580        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    } 
    143136} 
  • trunk/ddl/elf/ELFModule.d

    r220 r221  
    3636private import ddl.Attributes; 
    3737 
    38  
    3938private import ddl.elf.ELFBinary; 
     39private import ddl.elf.ELFReader; 
     40private import ddl.elf.ELFPrinter; 
    4041 
    4142/** 
     
    6970    this(FileBuffer buffer){ 
    7071        resolved = false; 
    71         loadBinary(new DDLReader(buffer)); 
     72        loadBinary(new ELFReader(buffer)); 
    7273    } 
    7374         
    74     this(DDLReader reader){ 
     75    this(ELFReader reader){ 
    7576        resolved = false; 
    7677        loadBinary(reader); 
     
    111112    } 
    112113     
    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//        } 
    117118         
    118119        //TODO: analyze  
     
    123124        ExtSprintClass sprint = new ExtSprintClass(1024); 
    124125     
    125         if(debug)
     126        debug
    126127            result = "ELF Binary Data: \n" ~ binary.toString(); 
    127128        } 
  • trunk/ddl/omf/OMFModule.d

    r220 r221  
    110110                //NOTE: namely, this includes __except_list and __nullext, which point to the start 
    111111                // of their respective segments 
    112                 *destAddress = fixupValue; 
     112                *cast(uint*)destAddress = fixupValue; 
    113113            } 
    114114            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 
    117116            } 
    118117            else{ // self relative 
    119                 *destAddress += fixupValue;            
     118                *cast(uint*)destAddress += fixupValue;             
    120119            } 
    121120        } 
     
    380379            newFix.isSegmentRelative = fix.isSegmentRelative; 
    381380            newFix.isExternStyleFixup = fix.isExternStyleFixup; 
     381            newFix.targetIndex = fix.targetIndex; 
    382382             
    383383            if(fix.destNameIndex > 0){ 
     
    423423                    char[] rel = isSegmentRelative ? "segmentRelative" : "selfRelative"; 
    424424                    char[] ext = isExternStyleFixup ? "externStyle" : "segmentStyle"; 
    425                     void* targetAddress = &(segmentImages[destSegmentIndex].data[destOffset]); 
    426425                             
    427                     result ~= sprint("  %d: %0.8X %s %s",idx,targetAddress,rel,ext); 
     426                    result ~= sprint("  %d: %0.8X %s %s",idx,destAddress,rel,ext); 
    428427     
    429428                    if(isExternStyleFixup){ 
     
    439438        result ~= "DATA: \n"; 
    440439        foreach(idx,segdef; segmentImages){ 
     440            char[] buf2 = ""; 
    441441            result ~= sprint("segment %d data (%d bytes): ",idx,segdef.data.length); 
    442442            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                } 
    446454            } 
    447455            result ~= "\n"; 
  • trunk/test/linktest1.d

    r219 r221  
    2828import ddl.all; 
    2929 
     30import std.stdio; 
     31 
    3032import mango.io.Stdout; 
    3133 
     
    4244private import test.testclassinterface; 
    4345 
    44 uint add(uint a,uint b){ 
    45     asm{ 
    46         nop; 
    47         nop; 
    48         nop; 
    49     } 
    50     return 0; 
    51 } 
    52  
    5346void main(){ 
    5447    Stdout.println("Starting."); 
     
    6255    auto testLibrary = linker.loadAndLink("test\\testmodule.obj"); 
    6356    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)); 
    7157     
    7258    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")(); 
    7476    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     
    7688    // Create a class using two different constructors. We need to specify 
    7789    // the interface we'll be using (in this case, ITestClass). 
  • trunk/test/testmodule.d

    r219 r221  
    2929import test.testclassinterface; 
    3030 
     31void* foobar = &printf; 
    3132 
     33public void* getPrintf(){ 
     34    return &printf; 
     35} 
    3236class TestClass : ITestClass { 
    3337    uint a; 
     
    7074} 
    7175 
     76public bool verifyPrintf(void* externalPrintf){  
     77    return &printf == externalPrintf; 
     78} 
     79 
     80char[] testString = "Hello DDL World!\n"; 
     81 
     82void* getTestString(){ 
     83    return &testString; 
     84} 
     85 
    7286public void helloWorld(){ 
     87    printf("hello world\n"); 
     88} 
     89 
     90void* getWritefln(){ 
     91    return &writefln; 
     92} 
     93 
     94public void helloWorld2(){ 
     95    writefln("hello world2\n"); 
     96} 
     97 
     98public void classTest(){ 
    7399    new TestClass(); 
    74     printf("Hello DDL World!\n"); 
    75100} 
    76101