Example #1
0
 public LinkerSection(SectionKind sectionKind, uint alignment)
 {
     SectionKind      = sectionKind;
     IsResolved       = false;
     SectionAlignment = alignment;
     Size             = 0;
 }
Example #2
0
 internal LinkerSymbol(string name, SectionKind kind, uint alignment)
 {
     Name = name;
     Alignment = alignment;
     SectionKind = kind;
     LinkRequests = new List<LinkRequest>();
 }
Example #3
0
 internal LinkerSymbol(string name, SectionKind kind, uint alignment)
 {
     Name         = name;
     Alignment    = alignment;
     SectionKind  = kind;
     LinkRequests = new List <LinkRequest>();
 }
Example #4
0
        public LinkerSymbol DefineSymbol(string name, SectionKind kind, int alignment, int size)
        {
            uint aligned = alignment != 0 ? (uint)alignment : 1;

            lock (_lock)
            {
                if (!symbolLookup.TryGetValue(name, out LinkerSymbol symbol))
                {
                    symbol = new LinkerSymbol(name, aligned, kind);

                    Symbols.Add(symbol);
                    symbolLookup.Add(name, symbol);
                    symbol.IsExternalSymbol = false;
                }

                symbol.Alignment   = aligned;
                symbol.SectionKind = kind;

                symbol.Stream = (size == 0) ? new MemoryStream() : new MemoryStream(size);

                if (size != 0)
                {
                    symbol.Stream.SetLength(size);
                }

                return(symbol);
            }
        }
Example #5
0
        private uint ResolveSymbolLocation(SectionKind section, ulong VirtualAddress)
        {
            uint position = 0;

            foreach (var symbol in Symbols)
            {
                if (symbol.IsReplaced)
                {
                    continue;
                }

                if (symbol.SectionKind != section)
                {
                    continue;
                }

                if (symbol.IsResolved)
                {
                    continue;
                }

                symbol.SectionOffset  = position;
                symbol.VirtualAddress = VirtualAddress + position;

                position += symbol.Size;
            }

            return(position);
        }
        /// <summary>
        /// Allocates a symbol of the given name in the specified section.
        /// </summary>
        /// <param name="name">The name of the symbol.</param>
        /// <param name="section">The executable section to allocate From.</param>
        /// <param name="size">The number of bytes to allocate. If zero, indicates an unknown amount of memory is required.</param>
        /// <param name="alignment">The alignment. A value of zero indicates the use of a default alignment for the section.</param>
        /// <returns>
        /// A stream, which can be used to populate the section.
        /// </returns>
        public virtual Stream Allocate(string name, SectionKind section, int size, int alignment)
        {
            try
            {
                Stream baseStream = Allocate(section, size, alignment);

                // Create a linker symbol for the name
                LinkerSymbol symbol = new LinkerSymbol(name, section, baseStream.Position);


                // Save the symbol for later use
                if (!symbols.ContainsKey(symbol.Name))
                {
                    symbols.Add(symbol.Name, symbol);
                }

                // Wrap the stream to catch premature disposal
                Stream result = new LinkerStream(symbol, baseStream, size);

                return(result);
            }
            catch (ArgumentException argx)
            {
                throw new LinkerException(String.Format(@"Symbol {0} defined multiple times.", name), argx);
            }
        }
Example #7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Elf32Section"/> class.
 /// </summary>
 /// <param name="kind">The kind of the section.</param>
 /// <param name="name">The name.</param>
 /// <param name="virtualAddress">The virtualAddress.</param>
 public Elf32Section(SectionKind kind, string name, IntPtr virtualAddress)
     : base(kind, name, virtualAddress)
 {
     _header = new Elf32SectionHeader();
     _header.Name = Elf32StringTableSection.AddString(name);
     _sectionStream = new System.IO.MemoryStream();
 }
Example #8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Elf32Section"/> class.
 /// </summary>
 /// <param name="kind">The kind of the section.</param>
 /// <param name="name">The name.</param>
 /// <param name="virtualAddress">The virtualAddress.</param>
 public Elf32Section(SectionKind kind, string name, IntPtr virtualAddress)
     : base(kind, name, virtualAddress)
 {
     _header        = new Elf32SectionHeader();
     _header.Name   = Elf32StringTableSection.AddString(name);
     _sectionStream = new System.IO.MemoryStream();
 }
Example #9
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Section"/> class.
 /// </summary>
 /// <param name="kind">The kind of the section.</param>
 /// <param name="name">The name.</param>
 /// <param name="virtualAddress">The virtualAddress.</param>
 public Section(SectionKind kind, string name, IntPtr virtualAddress)
     : base(kind, name, virtualAddress)
 {
     header = new SectionHeader();
     header.Name = StringTableSection.AddString(name);
     stream = new MemoryStream();
 }
Example #10
0
        public void Link(LinkType linkType, PatchType patchType, string patchSymbol, SectionKind patchKind, int patchOffset, int relativeBase, string referenceSymbol, SectionKind referenceKind, int referenceOffset)
        {
            var referenceObject = GetSymbol(referenceSymbol, referenceKind);
            var patchObject     = GetSymbol(patchSymbol, patchKind);

            Link(linkType, patchType, patchObject, patchOffset, relativeBase, referenceObject, referenceOffset);
        }
Example #11
0
        /// <summary>
        /// Allocates memory in the specified section.
        /// </summary>
        /// <param name="member">The metadata member to allocate space for.</param>
        /// <param name="section">The executable section to allocate From.</param>
        /// <param name="size">The number of bytes to allocate. If zero, indicates an unknown amount of memory is required.</param>
        /// <param name="alignment">The alignment. A value of zero indicates the use of a default alignment for the section.</param>
        /// <returns>A stream, which can be used to populate the section.</returns>
        public virtual Stream Allocate(RuntimeMember member, SectionKind section, int size, int alignment)
        {
            // Create a canonical symbol name
            string name = CreateSymbolName(member);

            // Create a stream
            return(Allocate(name, section, size, alignment));
        }
Example #12
0
 internal LinkerSymbol(string name, uint alignment = 0, SectionKind kind = SectionKind.Unknown)
 {
     Name         = name;
     Alignment    = alignment;
     SectionKind  = kind;
     LinkRequests = new List <LinkRequest>();
     IsExport     = false;
 }
Example #13
0
        public LinkerSymbol FindSymbol(string name, SectionKind kind)
        {
            var section = LinkerSections[(int)kind];

            Debug.Assert(section != null);

            return(section.GetSymbol(name));
        }
Example #14
0
 public void Read(BinaryReader reader)
 {
     blobIndex              = reader.ReadInt32();
     bankNameIndex          = reader.ReadInt32();
     startAddress           = reader.ReadInt64();
     kind                   = (SectionKind)reader.ReadInt16();
     customSectionNameIndex = reader.ReadInt32();
     accessMode             = (SectionAccessMode)reader.ReadInt16();
 }
Example #15
0
 public LinkerSection(SectionKind sectionKind, uint alignment)
 {
     SectionKind = sectionKind;
     IsResolved = false;
     symbolLookup = new Dictionary<string, LinkerSymbol>();
     Symbols = new List<LinkerSymbol>();
     SectionAlignment = alignment;
     Size = 0;
 }
Example #16
0
 public LinkerSection(SectionKind sectionKind, uint alignment)
 {
     SectionKind      = sectionKind;
     IsResolved       = false;
     symbolLookup     = new Dictionary <string, LinkerSymbol>();
     Symbols          = new List <LinkerSymbol>();
     SectionAlignment = alignment;
     Size             = 0;
 }
Example #17
0
        void ConfigureDataSource()
        {
            dataSource = new UICollectionViewDiffableDataSource <NSNumber, NSNumber> (collectionView, CellProviderHandler)
            {
                SupplementaryViewProvider = SupplementaryViewProviderHandler
            };

            // initial data
            var snapshot        = new NSDiffableDataSourceSnapshot <NSNumber, NSNumber> ();
            var idOffset        = 0;
            var itemsPerSection = 18;

            foreach (var section in SectionKind.AllSections)
            {
                snapshot.AppendSections(new [] { NSNumber.FromInt32(section.EnumValue) });
                var items = Enumerable.Range(idOffset, itemsPerSection).Select(i => NSNumber.FromInt32(i)).ToArray();
                snapshot.AppendItems(items);
                idOffset += itemsPerSection;
            }

            dataSource.ApplySnapshot(snapshot, false);

            UICollectionViewCell CellProviderHandler(UICollectionView collectionView, NSIndexPath indexPath, NSObject obj)
            {
                // Get a cell of the desired kind.
                var cell = collectionView.DequeueReusableCell(TextCell.Key, indexPath) as TextCell;

                // Populate the cell with our item description.
                cell.Label.Text = $"{indexPath.Section}, {indexPath.Row}";
                cell.ContentView.BackgroundColor    = UIColorExtensions.CornflowerBlue;
                cell.ContentView.Layer.BorderColor  = UIColor.Black.CGColor;
                cell.ContentView.Layer.BorderWidth  = 1;
                cell.ContentView.Layer.CornerRadius = 8;
                cell.Label.TextAlignment            = UITextAlignment.Center;
                cell.Label.Font = UIFont.GetPreferredFontForTextStyle(UIFontTextStyle.Title1);

                // Return the cell.
                return(cell);
            }

            UICollectionReusableView SupplementaryViewProviderHandler(UICollectionView collectionView, string kind, NSIndexPath indexPath)
            {
                var sectionKind = SectionKind.GetSectionKind(indexPath.Section);

                // Get a supplementary view of the desired kind.
                var header = collectionView.DequeueReusableSupplementaryView(new NSString(kind),
                                                                             TitleSupplementaryView.Key, indexPath) as TitleSupplementaryView;

                // Populate the view with our section's description.
                header.Label.Text = $".{sectionKind}";

                // Return the view.
                return(header);
            }
        }
Example #18
0
        private uint GetSectionLength(SectionKind sectionKind)
        {
            LinkerSection section;

            if (this.sections.TryGetValue(sectionKind, out section) == true && section.Length > 0)
            {
                return((uint)section.Length);
            }

            return(0);
        }
Example #19
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SimLinkerSection" /> class.
        /// </summary>
        /// <param name="kind">The kind of the section.</param>
        /// <param name="name">The name.</param>
        /// <param name="address">The address.</param>
        /// <param name="size">The size.</param>
        /// <param name="simAdapter">The sim adapter.</param>
        public SimLinkerSection(SectionKind kind, string name, uint address, uint size, ISimAdapter simAdapter)
            : base(kind, name, 0)
        {
            Memory = new byte[size];

            VirtualAddress = address;

            simAdapter.SimCPU.AddMemory(address, size, 1);

            stream = new MemoryStream(Memory, true);
        }
Example #20
0
        private long GetSectionAddress(SectionKind sectionKind)
        {
            LinkerSection section;

            if (this.sections.TryGetValue(sectionKind, out section) == true && section.Length > 0)
            {
                return((uint)section.VirtualAddress.ToInt64());
            }

            return(0L);
        }
Example #21
0
        /// <summary>
        /// Initializes a new instance of the <see cref="LinkerSymbol"/> class.
        /// </summary>
        /// <param name="name">The name of the symbol.</param>
        /// <param name="section">The section holding the symbol.</param>
        /// <param name="sectionAddress">Holds the section relative address of the symbol.</param>
        /// <exception cref="T:System.ArgumentException"><paramref name="name"/> is empty.</exception>
        /// <exception cref="T:System.ArgumentNullException"><paramref name="name"/> is null.</exception>
        public LinkerSymbol(string name, SectionKind section, long sectionAddress)
        {
            Debug.Assert(!String.IsNullOrEmpty(name), @"LinkerSymbol requires a proper name.");
            if (name == null)
                throw new ArgumentNullException(@"name");
            if (name.Length == 0)
                throw new ArgumentException(@"Name can't be empty.", @"name");

            this.name = name;
            this.section = section;
            this.sectionAddress = sectionAddress;
        }
Example #22
0
        private bool ContainsKind(SectionKind kind)
        {
            foreach (var symbol in linker.Symbols)
            {
                if (symbol.SectionKind == kind)
                {
                    return(true);
                }
            }

            return(false);
        }
Example #23
0
 private void AllocateSpace(RuntimeField field, SectionKind section, int size, int alignment)
 {
     using (Stream stream = linker.Allocate(field.ToString(), section, size, alignment))
     {
         if (field.RVA != 0)
         {
             InitializeStaticValueFromRVA(stream, size, field);
         }
         else
         {
             WriteDummyBytes(stream, size);
         }
     }
 }
Example #24
0
		public LinkerSymbol CreateSymbol(string name, SectionKind kind, int alignment, int size)
		{
			var symbol = CreateSymbol(name, kind, (uint)alignment);

			MemoryStream stream = (size == 0) ? new MemoryStream() : new MemoryStream(size);
			symbol.Stream = Stream.Synchronized(stream);

			if (size != 0)
			{
				stream.SetLength(size);
			}

			return symbol;
		}
Example #25
0
 private void AllocateSpace(IAssemblyLinker linker, RuntimeField field, SectionKind section, int size, int alignment)
 {
     using (Stream stream = linker.Allocate(field, section, size, alignment))
     {
         if (IntPtr.Zero != field.RVA)
         {
             InitializeStaticValueFromRVA(stream, size, field);
         }
         else
         {
             WriteDummyBytes(stream, size);
         }
     }
 }
Example #26
0
 private void AllocateSpace(MosaField field, SectionKind section, int size, int alignment)
 {
     using (var stream = Compiler.Linker.Allocate(field.FullName, section, size, alignment))
     {
         if (field.Data != null)
         {
             stream.Write(field.Data, 0, size);
         }
         else
         {
             stream.WriteZeroBytes(size);
         }
     }
 }
 private void AllocateSpace(RuntimeField field, SectionKind section, int size, int alignment)
 {
     using (Stream stream = linker.Allocate(field.ToString(), section, size, alignment))
     {
         if (field.RVA != 0)
         {
             InitializeStaticValueFromRVA(stream, size, field);
         }
         else
         {
             stream.WriteZeroBytes(size);
         }
     }
 }
Example #28
0
        private Stream EmitRelocationAddendSection(SectionKind kind)
        {
            var stream = new MemoryStream();
            var writer = new BinaryWriter(stream);

            foreach (var symbol in Linker.Symbols)
            {
                if (symbol.IsExternalSymbol)
                {
                    continue;
                }

                foreach (var patch in symbol.LinkRequests)
                {
                    if (patch.ReferenceOffset == 0)
                    {
                        continue;
                    }

                    if (patch.ReferenceSymbol.SectionKind != kind)
                    {
                        continue;
                    }

                    if (patch.LinkType == LinkType.Size)
                    {
                        continue;
                    }

                    if (!patch.ReferenceSymbol.IsExternalSymbol)                     // FUTURE: include relocations for static symbols, if option selected
                    {
                        continue;
                    }

                    var relocationAddendEntry = new RelocationAddendEntry()
                    {
                        RelocationType = ConvertType(MachineType, patch.LinkType, patch.PatchType),
                        Symbol         = symbolTableOffset[patch.ReferenceSymbol],
                        Offset         = (ulong)(symbol.SectionOffset + patch.PatchOffset),
                        Addend         = (ulong)patch.ReferenceOffset,
                    };

                    relocationAddendEntry.Write(LinkerFormatType, writer);
                }
            }

            return(stream);
        }
Example #29
0
        /// <summary>
        /// Initializes a new instance of the <see cref="LinkerSymbol"/> class.
        /// </summary>
        /// <param name="name">The name of the symbol.</param>
        /// <param name="section">The section holding the symbol.</param>
        /// <param name="sectionAddress">Holds the section relative address of the symbol.</param>
        /// <exception cref="T:System.ArgumentException"><paramref name="name"/> is empty.</exception>
        /// <exception cref="T:System.ArgumentNullException"><paramref name="name"/> is null.</exception>
        public LinkerSymbol(string name, SectionKind section, long sectionAddress)
        {
            Debug.Assert(!String.IsNullOrEmpty(name), @"LinkerSymbol requires a proper name.");
            if (name == null)
            {
                throw new ArgumentNullException(@"name");
            }
            if (name.Length == 0)
            {
                throw new ArgumentException(@"Name can't be empty.", @"name");
            }

            this.name           = name;
            this.section        = section;
            this.sectionAddress = sectionAddress;
        }
Example #30
0
        //   +-----------------------------------------------------+
        //   | +---------------------------------+  +-----------+  |
        //   | |                                 |  |           |  |
        //   | |                                 |  |           |  |
        //   | |                                 |  |     1     |  |
        //   | |                                 |  |           |  |
        //   | |                                 |  |           |  |
        //   | |                                 |  +-----------+  |
        //   | |               0                 |                 |
        //   | |                                 |  +-----------+  |
        //   | |                                 |  |           |  |
        //   | |                                 |  |           |  |
        //   | |                                 |  |     2     |  |
        //   | |                                 |  |           |  |
        //   | |                                 |  |           |  |
        //   | +---------------------------------+  +-----------+  |
        //   +-----------------------------------------------------+

        UICollectionViewLayout CreateLayout()
        {
            var config = new UICollectionViewCompositionalLayoutConfiguration {
                InterSectionSpacing = 20
            };

            return(new UICollectionViewCompositionalLayout(SectionProviderHandler, config));

            NSCollectionLayoutSection SectionProviderHandler(nint sectionIndex, INSCollectionLayoutEnvironment layoutEnvironment)
            {
                var sectionKind = SectionKind.GetSectionKind(sectionIndex);

                var leadingItemSize = NSCollectionLayoutSize.Create(NSCollectionLayoutDimension.CreateFractionalWidth(.7f),
                                                                    NSCollectionLayoutDimension.CreateFractionalHeight(1));
                var leadingItem = NSCollectionLayoutItem.Create(leadingItemSize);

                leadingItem.ContentInsets = new NSDirectionalEdgeInsets(10, 10, 10, 10);

                var trailingItemSize = NSCollectionLayoutSize.Create(NSCollectionLayoutDimension.CreateFractionalWidth(1),
                                                                     NSCollectionLayoutDimension.CreateFractionalHeight(.3f));
                var trailingItem = NSCollectionLayoutItem.Create(trailingItemSize);

                trailingItem.ContentInsets = new NSDirectionalEdgeInsets(10, 10, 10, 10);

                var trailingGroupSize = NSCollectionLayoutSize.Create(NSCollectionLayoutDimension.CreateFractionalWidth(.3f),
                                                                      NSCollectionLayoutDimension.CreateFractionalHeight(1));
                var trailingGroup = NSCollectionLayoutGroup.CreateVertical(trailingGroupSize, trailingItem, 2);

                var orthogonallyScrolls           = sectionKind.GetOrthogonalScrollingBehavior() != UICollectionLayoutSectionOrthogonalScrollingBehavior.None;
                var containerGroupFractionalWidth = orthogonallyScrolls ? .85f : 1;
                var containerGroupSize            = NSCollectionLayoutSize.Create(NSCollectionLayoutDimension.CreateFractionalWidth(containerGroupFractionalWidth),
                                                                                  NSCollectionLayoutDimension.CreateFractionalHeight(.4f));
                var containerGroup = NSCollectionLayoutGroup.CreateHorizontal(containerGroupSize, leadingItem, trailingGroup);

                var section = NSCollectionLayoutSection.Create(containerGroup);

                section.OrthogonalScrollingBehavior = sectionKind.GetOrthogonalScrollingBehavior();

                var sectionHeaderSize = NSCollectionLayoutSize.Create(NSCollectionLayoutDimension.CreateFractionalWidth(1),
                                                                      NSCollectionLayoutDimension.CreateAbsolute(44));
                var sectionHeader = NSCollectionLayoutBoundarySupplementaryItem.Create(sectionHeaderSize, headerElementKind, NSRectAlignment.Top);

                section.BoundarySupplementaryItems = new [] { sectionHeader };

                return(section);
            }
        }
Example #31
0
        private void RegisterRelocationSection(SectionKind kind, bool addend)
        {
            var relocationSection = new Section()
            {
                Name      = (addend ? ".rela" : ".rel") + SectionNames[(int)kind],
                Type      = addend ? SectionType.RelocationA : SectionType.Relocation,
                Link      = symbolSection,
                Info      = GetSection(kind),
                EntrySize = addend ? RelocationAddendEntry.GetEntrySize(LinkerFormatType) : RelocationEntry.GetEntrySize(LinkerFormatType),
                Emitter   = () => { return(addend ? EmitRelocationAddendSection(kind) : EmitRelocationSection(kind)); }
            };

            RegisterSection(relocationSection);

            relocationSection.AddDependency(symbolSection);
            relocationSection.AddDependency(GetSection(kind));
        }
Example #32
0
        protected void CreateRelocationSection(SectionKind kind, bool addend)
        {
            var relocationSection = new Section();

            relocationSection.Name             = (addend ? ".rela" : ".rel") + LinkerSectionNames[(int)kind];
            relocationSection.Type             = addend ? SectionType.RelocationA : SectionType.Relocation;
            relocationSection.Link             = symbolSection;
            relocationSection.Info             = GetSection(kind);
            relocationSection.AddressAlignment = linker.SectionAlignment;
            relocationSection.EntrySize        = (uint)(addend ? RelocationAddendEntry.GetEntrySize(linkerFormatType) : RelocationEntry.GetEntrySize(linkerFormatType));
            relocationSection.EmitMethod       = WriteRelocationSection;

            AddSection(relocationSection);

            relocationSection.AddDependency(symbolSection);
            relocationSection.AddDependency(GetSection(kind));
        }
Example #33
0
        private void CreateRelocationSection(SectionKind kind, bool addend)
        {
            var relocationSection = new Section()
            {
                Name             = (addend ? ".rela" : ".rel") + LinkerSectionNames[(int)kind],
                Type             = addend ? SectionType.RelocationA : SectionType.Relocation,
                Link             = symbolSection,
                Info             = GetSection(kind),
                AddressAlignment = linker.SectionAlignment,
                EntrySize        = addend ? RelocationAddendEntry.GetEntrySize(linkerFormatType) : RelocationEntry.GetEntrySize(linkerFormatType),
                EmitMethod       = WriteRelocationSection
            };

            AddSection(relocationSection);

            relocationSection.AddDependency(symbolSection);
            relocationSection.AddDependency(GetSection(kind));
        }
Example #34
0
        /// <summary>
        /// Allocates a symbol of the given name in the specified section.
        /// </summary>
        /// <param name="name">The name of the symbol.</param>
        /// <param name="section">The executable section to allocate from.</param>
        /// <param name="size">The number of bytes to allocate. If zero, indicates an unknown amount of memory is required.</param>
        /// <param name="alignment">The alignment. A value of zero indicates the use of a default alignment for the section.</param>
        /// <returns>
        /// A stream, which can be used to populate the section.
        /// </returns>
        public override Stream Allocate(string name, SectionKind section, int size, int alignment)
        {
            LinkerStream stream = (LinkerStream)base.Allocate(name, section, size, alignment);

            try
            {
                VirtualMemoryStream vms    = (VirtualMemoryStream)stream.BaseStream;
                LinkerSymbol        symbol = this.GetSymbol(name);
                symbol.VirtualAddress = new IntPtr(vms.Base.ToInt64() + vms.Position);
            }
            catch
            {
                stream.Dispose();
                throw;
            }

            return(stream);
        }
Example #35
0
        public LinkerSymbol DefineExternalSymbol(string name, string externalName, SectionKind kind)
        {
            lock (_lock)
            {
                if (!symbolLookup.TryGetValue(name, out LinkerSymbol symbol))
                {
                    symbol = new LinkerSymbol(name, 0, kind);

                    Symbols.Add(symbol);
                    symbolLookup.Add(name, symbol);
                }

                symbol.SectionKind = kind;
                symbol.IsExport    = true;
                symbol.ExportName  = externalName;

                return(symbol);
            }
        }
Example #36
0
        /// <summary>
        /// Allocates a symbol of the given name in the specified section.
        /// </summary>
        /// <param name="name">The name of the symbol.</param>
        /// <param name="section">The executable section to allocate from.</param>
        /// <param name="size">The number of bytes to allocate. If zero, indicates an unknown amount of memory is required.</param>
        /// <param name="alignment">The alignment. A value of zero indicates the use of a default alignment for the section.</param>
        /// <returns>
        /// A stream, which can be used to populate the section.
        /// </returns>
        public override Stream Allocate(string name, SectionKind section, int size, int alignment)
        {
            LinkerStream stream = (LinkerStream)base.Allocate(name, section, size, alignment);

            try
            {
                VirtualMemoryStream vms    = (VirtualMemoryStream)stream.BaseStream;
                LinkerSymbol        symbol = GetSymbol(name);
                symbol.VirtualAddress = new IntPtr(vms.Base.ToInt64() + vms.Position);
                Trace.WriteLine("Symbol " + name + " located at 0x" + symbol.VirtualAddress.ToInt32().ToString("x08"));
            }
            catch
            {
                stream.Dispose();
                throw;
            }

            return(stream);
        }
        /// <summary>
        /// Emits the displacement operand.
        /// </summary>
        /// <param name="displacement">The displacement operand.</param>
        private void WriteDisplacement(Operand displacement)
        {
            if (displacement.IsLabel)
            {
                // FIXME! remove assertion
                Debug.Assert(displacement.Displacement == 0);

                linker.Link(LinkType.AbsoluteAddress, BuiltInPatch.I4, MethodName, SectionKind.Text, (int)codeStream.Position, 0, displacement.Name, SectionKind.ROData, 0);
                codeStream.WriteZeroBytes(4);
            }
            else if (displacement.IsField)
            {
                SectionKind section = displacement.Field.Data != null ? section = SectionKind.ROData : section = SectionKind.BSS;
                linker.Link(LinkType.AbsoluteAddress, BuiltInPatch.I4, MethodName, SectionKind.Text, (int)codeStream.Position, 0, displacement.Field.FullName, section, (int)displacement.Displacement);
                codeStream.WriteZeroBytes(4);
            }
            else if (displacement.IsSymbol)
            {
                // FIXME! remove assertion
                Debug.Assert(displacement.Displacement == 0);

                SectionKind section = (displacement.Method != null) ? SectionKind.Text : SectionKind.ROData;

                var symbol = linker.FindSymbol(displacement.Name);
                if (symbol == null)
                {
                    symbol = linker.GetSymbol(displacement.Name, section);
                }

                linker.Link(LinkType.AbsoluteAddress, BuiltInPatch.I4, MethodName, SectionKind.Text, (int)codeStream.Position, 0, symbol.Name, symbol.SectionKind, 0);
                codeStream.WriteZeroBytes(4);
            }
            else if (displacement.IsMemoryAddress && displacement.OffsetBase != null && displacement.OffsetBase.IsConstant)
            {
                codeStream.Write((int)(displacement.OffsetBase.ConstantSignedLongInteger + displacement.Displacement), Endianness.Little);
            }
            else
            {
                codeStream.Write((int)displacement.Displacement, Endianness.Little);
            }
        }
Example #38
0
        /// <summary>
        /// Allocates a symbol of the given name in the specified section.
        /// </summary>
        /// <param name="name">The name of the symbol.</param>
        /// <param name="section">The executable section to allocate From.</param>
        /// <param name="size">The number of bytes to allocate. If zero, indicates an unknown amount of memory is required.</param>
        /// <param name="alignment">The alignment. A value of zero indicates the use of a default alignment for the section.</param>
        /// <returns>
        /// A stream, which can be used to populate the section.
        /// </returns>
        public virtual Stream Allocate(string name, SectionKind section, int size, int alignment)
        {
            Stream stream = Allocate(section, size, alignment);

            try
            {
                // Create a linker symbol for the name
                LinkerSymbol symbol = new LinkerSymbol(name, section, stream.Position);

                // Save the symbol for later use
                _symbols.Add(symbol.Name, symbol);

                // Wrap the stream to catch premature disposal
                stream = new LinkerStream(symbol, stream, size);
            } catch (ArgumentException argx)
            {
                throw new LinkerException(String.Format("Symbol {0} defined multiple times.", name), argx);
            }

            return(stream);
        }
Example #39
0
 public LinkerSymbol GetSymbol(string name, SectionKind kind)
 {
     return CreateSymbol(name, kind, 0);
 }
Example #40
0
        public LinkerSymbol CreateSymbol(string name, SectionKind kind, int alignment, int size)
        {
            var symbol = CreateSymbol(name, kind, (uint)alignment);

            MemoryStream stream = (size == 0) ? new MemoryStream() : new MemoryStream(size);
            symbol.Stream = Stream.Synchronized(stream);

            if (size != 0)
            {
                stream.SetLength(size);
            }

            return symbol;
        }
Example #41
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Section"/> class.
 /// </summary>
 /// <param name="kind">The kind of the section.</param>
 /// <param name="name">The name of the section.</param>
 /// <param name="virtualAddress">The virtualAddress of the section.</param>
 public Section(SectionKind kind, string name, IntPtr virtualAddress)
     : base(kind, name, virtualAddress)
 {
     stream = new MemoryStream();
 }
Example #42
0
 /// <summary>
 /// Allocates a symbol of the given name in the specified section.
 /// </summary>
 /// <param name="section">The executable section to allocate From.</param>
 /// <param name="size">The number of bytes to allocate. If zero, indicates an unknown amount of memory is required.</param>
 /// <param name="alignment">The alignment. A value of zero indicates the use of a default alignment for the section.</param>
 /// <returns>
 /// A stream, which can be used to populate the section.
 /// </returns>
 protected override System.IO.Stream Allocate(SectionKind section, int size, int alignment)
 {
     Elf64.Sections.Elf64Section linkerSection = (Elf64.Sections.Elf64Section)GetSection(section);
     return linkerSection.Allocate(size, alignment);
 }
 /// <summary>
 /// Allocates a symbol of the given name in the specified section.
 /// </summary>
 /// <param name="section">The executable section to allocate From.</param>
 /// <param name="size">The number of bytes to allocate. If zero, indicates an unknown amount of memory is required.</param>
 /// <param name="alignment">The alignment. A value of zero indicates the use of a default alignment for the section.</param>
 /// <returns>
 /// A stream, which can be used to populate the section.
 /// </returns>
 protected abstract Stream Allocate(SectionKind section, int size, int alignment);
Example #44
0
 /// <summary>
 /// Retrieves a linker section by its type.
 /// </summary>
 /// <param name="sectionKind">The type of the section to retrieve.</param>
 /// <returns>The retrieved linker section.</returns>
 public override LinkerSection GetSection(SectionKind sectionKind)
 {
     return this.sections[(int)sectionKind];
 }
Example #45
0
        protected LinkerSymbol CreateSymbol(string name, SectionKind kind, uint alignment)
        {
            lock (mylock)
            {
                var section = Sections[(int)kind];

                Debug.Assert(section != null);

                var symbol = section.GetSymbol(name);

                if (symbol == null)
                {
                    symbol = new LinkerSymbol(name, kind, alignment);

                    section.AddLinkerObject(symbol);
                }

                symbol.Alignment = alignment != 0 ? alignment : 0;

                return symbol;
            }
        }
        private uint GetSectionLength(SectionKind sectionKind)
        {
            LinkerSection section;
            if (this.sections.TryGetValue(sectionKind, out section) == true && section.Length > 0)
                return (uint)section.Length;

            return 0;
        }
 /// <summary>
 /// Allocates memory in the specified section.
 /// </summary>
 /// <param name="symbol">The metadata member to allocate space for.</param>
 /// <param name="section">The executable section to allocate From.</param>
 /// <param name="size">The number of bytes to allocate. If zero, indicates an unknown amount of memory is required.</param>
 /// <param name="alignment">The alignment. A value of zero indicates the use of a default alignment for the section.</param>
 /// <returns>A stream, which can be used to populate the section.</returns>
 public Stream Allocate(RuntimeMember symbol, SectionKind section, int size, int alignment)
 {
     CheckImplementation();
     return this.implementation.Allocate(symbol, section, size, alignment);
 }
 /// <summary>
 /// Gets the section.
 /// </summary>
 /// <param name="sectionKind">Kind of the section.</param>
 /// <returns>The section of the requested kind.</returns>
 public LinkerSection GetSection(SectionKind sectionKind)
 {
     CheckImplementation();
     return this.implementation.GetSection(sectionKind);
 }
 /// <summary>
 /// Allocates a symbol of the given name in the specified section.
 /// </summary>
 /// <param name="name">The name of the symbol.</param>
 /// <param name="section">The executable section to allocate From.</param>
 /// <param name="size">The number of bytes to allocate. If zero, indicates an unknown amount of memory is required.</param>
 /// <param name="alignment">The alignment. A value of zero indicates the use of a default alignment for the section.</param>
 /// <returns>A stream, which can be used to populate the section.</returns>
 public Stream Allocate(string name, SectionKind section, int size, int alignment)
 {
     CheckImplementation();
     return this.implementation.Allocate(name, section, size, alignment);
 }
Example #50
0
 /// <summary>
 /// Retrieves a linker section by its type.
 /// </summary>
 /// <param name="sectionKind">The type of the section to retrieve.</param>
 /// <returns>The retrieved linker section.</returns>
 public override LinkerSection GetSection(SectionKind sectionKind)
 {
     throw new NotImplementedException();
 }
Example #51
0
 /// <summary>
 /// Allocates a symbol of the given name in the specified section.
 /// </summary>
 /// <param name="section">The executable section to allocate From.</param>
 /// <param name="size">The number of bytes to allocate. If zero, indicates an unknown amount of memory is required.</param>
 /// <param name="alignment">The alignment. A value of zero indicates the use of a default alignment for the section.</param>
 /// <returns>
 /// A stream, which can be used to populate the section.
 /// </returns>
 protected override Stream Allocate(SectionKind section, int size, int alignment)
 {
     throw new NotImplementedException();
 }
Example #52
0
        public void Link(LinkType linkType, PatchType patchType, string patchSymbol, SectionKind patchKind, int patchOffset, int relativeBase, string referenceSymbol, SectionKind referenceKind, int referenceOffset)
        {
            var referenceObject = GetSymbol(referenceSymbol, referenceKind);
            var patchObject = GetSymbol(patchSymbol, patchKind);

            Link(linkType, patchType, patchObject, patchOffset, relativeBase, referenceObject, referenceOffset);
        }
Example #53
0
        public void Link(LinkType linkType, PatchType patchType, LinkerSymbol patchSymbol, int patchOffset, int relativeBase, string referenceSymbol, SectionKind patchRelativeBase, int referenceOffset)
        {
            var referenceObject = GetSymbol(referenceSymbol, patchRelativeBase);

            Link(linkType, patchType, patchSymbol, patchOffset, relativeBase, referenceObject, referenceOffset);
        }
        /// <summary>
        /// Allocates a symbol of the given name in the specified section.
        /// </summary>
        /// <param name="name">The name of the symbol.</param>
        /// <param name="section">The executable section to allocate From.</param>
        /// <param name="size">The number of bytes to allocate. If zero, indicates an unknown amount of memory is required.</param>
        /// <param name="alignment">The alignment. A value of zero indicates the use of a default alignment for the section.</param>
        /// <returns>
        /// A stream, which can be used to populate the section.
        /// </returns>
        public virtual Stream Allocate(string name, SectionKind section, int size, int alignment)
        {
            Stream result;
            Stream baseStream = Allocate(section, size, alignment);

            try
            {
                // Create a linker symbol for the name
                LinkerSymbol symbol = new LinkerSymbol(name, section, baseStream.Position);

                // Save the symbol for later use
                _symbols.Add(symbol.Name, symbol);

                // Wrap the stream to catch premature disposal
                result = new LinkerStream(symbol, baseStream, size);
            }
            catch (ArgumentException argx)
            {
                throw new LinkerException(String.Format(@"Symbol {0} defined multiple times.", name), argx);
            }

            return result;
        }
Example #55
0
 protected LinkerSection GetSection(SectionKind kind)
 {
     return Sections[(int)kind];
 }
Example #56
0
 /// <summary>
 /// Allocates the specified member.
 /// </summary>
 /// <param name="member">The member.</param>
 /// <param name="section">The section.</param>
 /// <param name="size">The size.</param>
 /// <param name="alignment">The alignment.</param>
 /// <returns></returns>
 public virtual Stream Allocate(RuntimeMember member, SectionKind section, int size, int alignment)
 {
     return null;
 }
        private long GetSectionAddress(SectionKind sectionKind)
        {
            LinkerSection section;
            if (this.sections.TryGetValue(sectionKind, out section) == true && section.Length > 0)
            {
                return (uint)section.VirtualAddress.ToInt64();
            }

            return 0L;
        }
 /// <summary>
 /// Gets the section.
 /// </summary>
 /// <param name="sectionKind">Kind of the section.</param>
 /// <returns>The section of the requested kind.</returns>
 public abstract LinkerSection GetSection(SectionKind sectionKind);
Example #59
0
 /// <summary>
 /// Allocates a symbol of the given name in the specified section.
 /// </summary>
 /// <param name="name">The name of the symbol.</param>
 /// <param name="section">The executable section to allocate From.</param>
 /// <param name="size">The number of bytes to allocate. If zero, indicates an unknown amount of memory is required.</param>
 /// <param name="alignment">The alignment. A value of zero indicates the use of a default alignment for the section.</param>
 /// <returns>
 /// A stream, which can be used to populate the section.
 /// </returns>
 public virtual Stream Allocate(string name, SectionKind section, int size, int alignment)
 {
     return null;
 }
Example #60
0
 /// <summary>
 /// Allocates a symbol of the given name in the specified section.
 /// </summary>
 /// <param name="section">The executable section to allocate From.</param>
 /// <param name="size">The number of bytes to allocate. If zero, indicates an unknown amount of memory is required.</param>
 /// <param name="alignment">The alignment. A value of zero indicates the use of a default alignment for the section.</param>
 /// <returns>
 /// A stream, which can be used to populate the section.
 /// </returns>
 protected override Stream Allocate(SectionKind section, int size, int alignment)
 {
     Section linkerSection = (Section)GetSection(section);
     return linkerSection.Allocate(size, alignment);
 }