Пример #1
0
        public void ResetTest()
        {
            var objectFile = new ObjectFileMock();
            Context context = new Context(objectFile);

            Section section = new Section("Test");
            var symbol = new Symbol(SymbolType.Public, "test");
            var relocation = new Relocation(symbol, section, 0, 0, RelocationType.Default32);

            context.SymbolTable.Add(symbol);
            context.RelocationTable.Add(relocation);
            context.Section = section;
            context.Address = 123456789;
            Assert.AreEqual(symbol, context.SymbolTable[0]);
            Assert.AreEqual(relocation, context.RelocationTable[0]);
            Assert.AreEqual(section, context.Section);
            Assert.AreEqual((Int128)123456789, context.Address);

            context.Reset();

            // These have not changed
            Assert.AreEqual(symbol, context.SymbolTable[0]);
            Assert.AreEqual(relocation, context.RelocationTable[0]);

            // These are reset
            Assert.AreEqual(null, context.Section);
            Assert.AreEqual((Int128)0, context.Address);
        }
Пример #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Relocation"/> class.
 /// </summary>
 /// <param name="symbol">The target symbol.</param>
 /// <param name="offset">The offset relative to the start of at which the storage
 /// unit to be relocated resides.</param>
 /// <param name="addend">The constant used to compute the value of the relocatable field.</param>
 /// <param name="type">The type of relocation compution to perform.</param>
 public Relocation(Symbol symbol, long offset, long addend, RelocationType type)
 {
     TargetSymbol = symbol;
     Offset = offset;
     Addend = addend;
     Type = type;
 }
Пример #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Reference"/> class that references an associatable object.
        /// </summary>
        /// <param name="associatable">The <see cref="IAssociatable"/> being referenced.</param>
        /// <remarks>
        /// When the <see cref="IAssociatable"/> changes its <see cref="IAssociatable.AssociatedSymbol"/>,
        /// this reference is <em>not</em> updated or changed to reflect that, and will still be pointing at the
        /// previous, obsolete symbol.
        /// </remarks>
        public Reference(IAssociatable associatable)
        {
            #region Contract
            Contract.Requires<ArgumentNullException>(associatable != null);
            #endregion

            this.symbol = associatable.AssociatedSymbol;
        }
Пример #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Reference"/> class that references a symbol.
        /// </summary>
        /// <param name="symbol">The <see cref="Symbol"/> being referenced.</param>
        public Reference(Symbol symbol)
        {
            #region Contract
            Contract.Requires<ArgumentNullException>(symbol != null);
            #endregion

            this.symbol = symbol;
        }
        public void Constructor_IEnumerable()
        {
            var symbols = new Symbol[]{
                new Symbol(SymbolType.Private, "id1"),
                new Symbol(SymbolType.Public, "id2")
            };

            SymbolTable table = new SymbolTable(symbols);
            Assert.AreEqual(symbols.Length, table.Count);
            Assert.AreEqual(symbols, table.ToArray());
        }
Пример #6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Relocation"/> class.
        /// </summary>
        /// <param name="symbol">The target symbol.</param>
        /// <param name="section">The section in which the storage unit to be relocated resides.</param>
        /// <param name="offset">The offset relative to the start of <paramref name="section"/> at which the storage
        /// unit to be relocated resides.</param>
        /// <param name="addend">The constant used to compute the value of the relocatable field.</param>
        /// <param name="type">The type of relocation compution to perform.</param>
        public Relocation(Symbol symbol, Section section, Int128 offset, Int128 addend, RelocationType type)
        {
            #region Contract
            Contract.Requires<ArgumentNullException>(symbol != null);
            Contract.Requires<ArgumentNullException>(section != null);
            #endregion

            this.targetSymbol = symbol;
            this.section = section;
            this.offset = offset;
            this.addend = addend;
            this.type = type;
        }
        public void AddRange()
        {
            var symbols = new Symbol[]{
                new Symbol(SymbolType.Private, "id1"),
                new Symbol(SymbolType.Public, "id2")
            };

            SymbolTable table = new SymbolTable();
            Assert.AreEqual(0, table.Count);
            table.AddRange(symbols);

            Assert.AreEqual(symbols.Length, table.Count);
            Assert.AreEqual(symbols, table.ToArray());
        }
Пример #8
0
        public void ResolveTest()
        {
            var symbol1 = new Symbol(SymbolType.Private, "id1");
            var symbol2 = new Symbol(SymbolType.Private, "id2");

            Context context = new Context(new ObjectFileMock());

            var reference1 = new Reference("id1");
            var reference2 = new Reference(symbol2);

            Assert.IsFalse(reference1.Resolved);
            Assert.IsFalse(reference1.Resolve(context));
            context.SymbolTable.Add(symbol1);
            Assert.IsTrue(reference1.Resolve(context));
            Assert.IsTrue(reference1.Resolved);

            Assert.IsFalse(reference2.Resolved);
            Assert.IsFalse(reference2.Resolve(context));
            context.SymbolTable.Add(symbol2);
            Assert.IsTrue(reference2.Resolve(context));
            Assert.IsTrue(reference2.Resolved);
        }
        /// <inheritdoc />
        public override void Assemble(BinaryWriter writer)
        {
            // CONTRACT: ObjectFile

            // TODO: Emit .bss after the last progbits section.

            // Create a new context.
            Context context = this.ObjectFile.Architecture.CreateContext(this.ObjectFile);

            // Construct each section.
            context.Reset();
            context.Address = 0;		// Addresses relative to file.
            foreach (Section section in this.ObjectFile.Sections)
            {
                context.Section = section;
                string sectionName = String.Format("section.{0}.start", section.Identifier);
                var symbol = new Symbol(SymbolType.Private, sectionName);
                symbol.Define(context, context.Address);

                section.Construct(context);
            }

            // Emit each section and write it directly to the writer.
            context.Reset();
            context.Address = 0;		// Addresses relative to file.
            foreach (Section section in this.ObjectFile.Sections)
            {
                MathExt.CalculatePadding(writer.BaseStream.Position, section.Alignment);
                writer.Align(section.Alignment);
                section.Emit(writer, context);
            }

            // Test for illegal symbols.
            CheckSymbolSupport(context);

            writer.Flush();
        }
Пример #10
0
 /// <summary>
 /// Sets an association between an object and a symbol.
 /// </summary>
 /// <param name="symbol">The associated symbol; or <see langword="null"/>.</param>
 /// <param name="associatable">The associated object; or <see langword="null"/>.</param>
 public static void SetAssociation(Symbol symbol, IAssociatable associatable)
 {
     SetAssociation(associatable, symbol);
 }
Пример #11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Label"/> class
 /// that defines the specified symbol.
 /// </summary>
 /// <param name="symbol">The symbol that is defined.</param>
 /// <remarks>
 /// The <see cref="DefinedSymbol"/> property holds the symbol that is defined.
 /// </remarks>
 public Label(Symbol symbol)
 {
     DefinedSymbol = symbol;
 }
Пример #12
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Reference"/> class that references a symbol.
 /// </summary>
 /// <param name="symbol">The <see cref="Symbol"/> being referenced.</param>
 public Reference(Symbol symbol)
 {
     if (symbol == null)
         throw new ArgumentNullException(nameof(symbol));
     Symbol = symbol;
 }
Пример #13
0
        /// <summary>
        /// Attempts to resolve the reference.
        /// </summary>
        /// <param name="context">The <see cref="Context"/> to use.</param>
        /// <returns><see langword="true"/> when the <see cref="Reference"/> now contains a resolved reference;
        /// otherwise, <see langword="false"/> when it contains an unresolved reference.</returns>
        public bool Resolve(Context context)
        {
            // Is the reference already resolved?
            if (resolved)
                return true;

            // Do we reference a particular symbol by instance, or by identifier?
            if (symbol != null)
                // By instance. Then that symbol instance must be in the symbol table.
                return context.SymbolTable.Contains(symbol);
            else
            {
                // By identifier. Then a symbol with that identifier must be in the symbol table.
                symbol = context.SymbolTable[symbolIdentifier];
                return symbol != null;
            }
        }
Пример #14
0
 /// <inheritdoc />
 void IAssociatable.SetAssociatedSymbol(Symbol symbol)
 {
     this.associatedSymbol = symbol;
 }
Пример #15
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Label"/> class
 /// that defines the specified symbol.
 /// </summary>
 /// <param name="symbol">The symbol that is defined.</param>
 /// <remarks>
 /// The <see cref="DefinedSymbol"/> property holds the symbol that is defined.
 /// </remarks>
 public Label(Symbol symbol)
 {
     this.DefinedSymbol = symbol;
 }
Пример #16
0
        /// <summary>
        /// Sets an association between an object and a symbol.
        /// </summary>
        /// <param name="associatable">The associated object; or <see langword="null"/>.</param>
        /// <param name="symbol">The associated symbol; or <see langword="null"/>.</param>
        public static void SetAssociation(IAssociatable associatable, Symbol symbol)
        {
            if (symbol != null && associatable != null)
            {
                // Remove the associations between the specified symbol and its associatable,
                // and the specified associatable and its symbol.
                SetAssociation(symbol.association, null);
                SetAssociation(associatable, null);

                symbol.association = associatable;
                associatable.SetAssociatedSymbol(symbol);
            }
            else if (symbol != null)
            {
                if (symbol.association != null)
                    symbol.association.SetAssociatedSymbol(null);
                symbol.association = null;
            }
            else if (associatable != null)
            {
                if (associatable.AssociatedSymbol != null)
                    associatable.AssociatedSymbol.association = null;
                associatable.SetAssociatedSymbol(null);
            }
            // Else: both are null. We don't have to associate a null object with a null Symbol.
        }
Пример #17
0
 /// <inheritdoc />
 void IAssociatable.SetAssociatedSymbol(Symbol symbol)
 {
     definedSymbol = symbol;
 }