Merge pull request #980 from StephenMcConnel/bug-18638
[mono.git] / mcs / class / PEAPI / PEAPI.cs
index 30c2333b2e67f7f7e9d0c97b052317741467fe85..46672255e04174e3e5d7a4fcaa057c17b3588dcd 100644 (file)
@@ -162,8 +162,10 @@ namespace PEAPI {
 
                private uint GetNextSectStart(uint rva, uint tide) 
                {
-                       if (tide < SectionAlignment) return rva + SectionAlignment;
-                       return rva + ((tide / SectionAlignment) + 1) * SectionAlignment;
+                       uint c = tide / SectionAlignment;
+                       if ((tide % SectionAlignment) != 0)
+                               c++;
+                       return rva + (c * SectionAlignment);
                }
 
                private void BuildTextSection() 
@@ -588,8 +590,6 @@ namespace PEAPI {
                private static readonly string mscorlibName = "mscorlib";
                private Module thisMod;
                private ClassDef moduleClass;
-               private ArrayList classRefList = new ArrayList();
-               private ArrayList classDefList = new ArrayList();
                private ArrayList resources = new ArrayList ();
                private Assembly thisAssembly;
                private static bool isMSCorlib;
@@ -753,6 +753,11 @@ namespace PEAPI {
                        return modRef;
                }
 
+               public ClassRef AddExternClass(string ns, string name, TypeAttr attrs, MetaDataElement declRef)
+               {
+                       return new ExternClassRef (attrs, ns, name, declRef, metaData);
+               }
+               
                /// <summary>
                /// Add a "global" method to this module
                /// </summary>
@@ -760,9 +765,14 @@ namespace PEAPI {
                /// <param name="retType">return type</param>
                /// <param name="pars">method parameters</param>
                /// <returns>a descriptor for this new "global" method</returns>
+               public MethodDef AddMethod (string name, Param ret_param, Param [] pars) 
+               {
+                       return moduleClass.AddMethod (name, ret_param, pars);
+               }
+               
                public MethodDef AddMethod(string name, Type retType, Param[] pars) 
                {
-                       return moduleClass.AddMethod(name,retType,pars);
+                       return AddMethod (name, new Param (ParamAttr.Default, "", retType), pars);
                }
 
                /// <summary>
@@ -774,9 +784,14 @@ namespace PEAPI {
                /// <param name="retType">return type</param>
                /// <param name="pars">method parameters</param>
                /// <returns>a descriptor for this new "global" method</returns>
+               public MethodDef AddMethod (MethAttr mAtts, ImplAttr iAtts, string name, Param ret_param, Param [] pars) 
+               {
+                       return moduleClass.AddMethod (mAtts, iAtts, name, ret_param, pars);
+               }
+
                public MethodDef AddMethod(MethAttr mAtts, ImplAttr iAtts, string name, Type retType, Param[] pars) 
                {
-                       return moduleClass.AddMethod(mAtts,iAtts,name,retType,pars);
+                       return AddMethod (mAtts, iAtts, name, new Param (ParamAttr.Default, "", retType), pars);
                }
 
                public MethodRef AddMethodToTypeSpec (Type item, string name, Type retType, Type[] pars) 
@@ -898,7 +913,7 @@ namespace PEAPI {
 
                public void AddGenericParam (GenParam param)
                {
-                       param.GetTypeSpec (metaData);
+                       metaData.AddToTable (MDTable.TypeSpec, param);
                }
 
                public FileRef AddFile(string fName, byte[] hashBytes, bool hasMetaData, bool entryPoint) 
@@ -922,6 +937,7 @@ namespace PEAPI {
                public void AddCustomAttribute (Method meth, byte [] data, MetaDataElement element)
                {
                        metaData.AddCustomAttribute (new CustomAttribute (element, meth, data));
+                       element.HasCustomAttr = true;
                }
 
                public void AddDeclSecurity (SecurityAction sec_action, byte [] data, MetaDataElement element)
@@ -929,6 +945,11 @@ namespace PEAPI {
                        metaData.AddDeclSecurity (new DeclSecurity (element, (ushort) sec_action, data));
                }
 
+               public void AddDeclSecurity (SecurityAction sec_action, PEAPI.PermissionSet ps, MetaDataElement element)
+               {
+                       metaData.AddDeclSecurity (new DeclSecurity_20 (element, (ushort) sec_action, ps));
+               }
+
                /// <summary>
                /// Add a managed resource from another assembly.
                /// </summary>
@@ -972,7 +993,8 @@ namespace PEAPI {
                /// Write out the PEFile (the "bake" function)
                /// </summary>
                public void WritePEFile() { /* the "bake" function */
-                       fileImage.ReserveStrongNameSignatureSpace = thisAssembly.HasPublicKey;
+                       if (thisAssembly != null)
+                               fileImage.ReserveStrongNameSignatureSpace = thisAssembly.HasPublicKey;
                        fileImage.MakeFile();
                }
 
@@ -1197,7 +1219,7 @@ namespace PEAPI {
        /// Error for invalid PE file
        /// </summary>
        public class PEFileException : System.Exception {
-               public PEFileException(string msg) : base("Error in PE File:  " + msg) { }
+               public PEFileException(string msg) : base(msg) { }
        }
 
        public class NotYetImplementedException : System.Exception  {