Thursday, September 17, 2009

Linkers and Loaders


== Linkers and Loaders ==
By John R. Levine. 2000. ISBN 1-55860-496-0

- linkers and loaders are part of the software toolkit for almost
as long as there have been computers.
- This book is for:
students, programmers, computer language designers and developers.
- All the linker writers in the world could probably fit in one room,
and half of them would already have this book because they reviewed
the manuscript.

Chapter 1. Linking and Loading

1.1 What do linkers and loaders do?
- Basic job of linker/loader: binds more abstract names to more
concrete names. (name management, address binding)

1.2 Address binding: A historical perspective
- Linker and Loader divides the job: Linker do part of address binding,
assign relative addresses. Loader do final step of assigning actual addresses.

1.3 Linking and Loading
- linker does 1) symbol resolution, loader does 2) program loading.
Either can do 3) relocation.
- There are linking loaders that do all 3 functions
- Both patch object code

- Two-pass linking: linking is fundamentally a 2-pass process:
step 1) collecting info, step 2) linking
object files + shared lib + normal lib + linker control files + cmd line args -->
(linker) -->
Debug symbol file + Executable file + link/load map
- Object code libraries
- Relocation and code modification

1.4 Compiler drivers
- assembly code --> object code --> link object code and library together
- Linker command languages. Ways of passing commands to a linker:
1) command line, 2) intermixed with obj files,
3) embedded in obj files, 4) separate config language.

1.5 Linking: A true-life example

Chapter 2. Architecture issues

- Architecture: 1) hardward (program addressing, instruction formats), 2) OS.

2.1 ABI (Application Binary Interfaces)
- procedure call etc.

2.2 Memory addresses
- Byte order & alignment
IBM/Motorola: big endian
Intel/DEC: little endian
- misalignment: fault, or loss of performance
- register. size: program address

2.3 Address formation
- clean: 360/370/390
- simple: SPARC (RISC): v8 (32-bit), v9 (64-bit)
similar to other RISC arch: MIPS, Alpha
- irregular: x86

2.4 Instruction formats
- opcode operand
- direct/register addressing, base/indexed addressing
- fixed/variable length instruction
SPARC: all 4 bytes
370: 2/4/6 bytes
x86: 1-14 bytes

2.5 Procedure calls and addressibility
- abandon direct addressing for shorter instructions at the cost of
more complicated programming.
- bootstrapping for non-direct addressing
- procedure calls
- stack frame
arguments/local variables - on stack
local/global static variables - on heap

Scalable System Design

  • Access rights for different roles

  • - one header file for each role v.s. one header file (with lots of if/else) for all roles
    - assign ID to each page, assign ID list to each role

  • Slicing system to layers

  • - DB access, business logic, HTML, CSS
    - uniqueness
    - reusability
    - accumulation of code/solution

  • System analysis

  • - Client requirements analysis
    - technical requirements analysis
    - risk points, risk control
    - configuration, separate control points from code
    - data dictionary, allow user to adjust this themselves

  • Communicating to clients

  • - guide/educate clients

  • Team building

  • - source control
    - no new assignment before end of current assignment period even if current assignment is done before schedule
    - grow together
    - mutual tech evaluation/no boss participation

    Friday, August 21, 2009

    Manipulate PDF in .NET

    To convert PDF into image, we have the GFL SDK/GFLAx library as discussed in an earlier post.

    Now the problem is how to convert image into PDF, or how to draw onto PDF. One needs to rely on 3rd party module for this function.

    PDFSharp (http://www.pdfsharp.net/) works well for this. It can create new PDF file, or draw text and image onto existing PDF files. It even can generate barcode image (but guess it can't do recognization). The current version is 1.3, providing both source and assembly download at sourceforge.net. The source code can be used in one's own application, unless is for commercial purpose and needs support. It is written from scratch in C#. The only limit is that it requires .NET version 2.0 or above. To use this in .NET 1.1 or from other framework such as J2EE/LAMP, I think one can do something like a web service call.

    Some people say in web applications PDFSharp cannot run under medium security level. I didn't have this problem, probably because I'm running it on a trusted server, so there is no security restriction.

    One last word: it seems that PDF is frequently used in business applications. Now we have these open source projects that allows PDF convertion to and from other formats. Good to have these.

    Wednesday, August 5, 2009

    Friday, July 24, 2009

    C# DataGrid custom paging

    In .NET 1.1 (I don't know the later version at this time), datagrid paging navigation is troublesome since it does not include link to the first and last page, and does not show the total number of pages. The pager row can contain no other information, which is also inconvenient when you want to put something there, e.g., a checkbox says "select all" for all checkboxes on the rows in the datagrid.

    This is a way of imitating the paging manually, and allows the freedom of specifying "First, Prev, Next, Last" links, as well as total pages and other information on the pager row.

    The "Prev, ..., 11, 12, ..., Next" part can be done with the following function. Then you can add "First" and "Last" as link buttons. You can then provide the page count as a Label. Alternatively, the following example also adds the page count as a simulation of the link button "Last".

    In .aspx.cs page put:

    /// <summary>
    /// This function writes a customized navigation bar for datagrid.
    ///
    /// Note that lblNext and lblPrev can be replaced with image icons.
    ///
    /// Example of Calling this function:
    /// writeDataGridNavBar(
    /// this.DataGrid1.PageCount,
    /// this.DataGrid1.CurrentPageIndex,
    /// this.DataGrid1.PagerStyle.PageButtonCount,
    /// 1
    /// );
    ///
    /// The last parameter "1" here is obtained this way:
    /// Check the aspx page with default paging, look at the links to "1, 2, ...",
    /// javascript:__doPostBack('DataGrid1$_ctl1$_ctl4','')
    /// ^
    /// Pass this number as the last parameter.
    ///
    /// The optional ctrl_LastPage in the code below is similarly obtained from the
    /// "Last" link button.
    ///
    /// </summary>
    /// <param name="totalPage">DataGrid.PageCount</param>
    /// <param name="currentPage">DataGrid.CurrentPageIndex</param>
    /// <param name="pageButtonCount">DataGrid.PagerStyle.PageButtonCount</param>
    /// <param name="ctl_val"></param>
    /// <returns></returns>
    ///
    /// @Author: HomeTom
    /// @Date: 7/24/2009
    ///
    public string writeDataGridNavBar(
    int PageCount, int CurrentPageIndex, int PageButtonCount, int ctl_val)
    {
    string lblNext = "Next";
    string lblPrev = "Prev";
    string ctrl = "javascript:__doPostBack('DataGrid1$_ctl" + ctl_val + "$_ctl";
    // Optional. Use this only when use the "Last" link button.
    string ctrl_LastPage = "javascript:__doPostBack('btnLastPage','')";
    string s = "";
    int i, j, tmp;

    int startPage =
    ((int) (Math.Floor((CurrentPageIndex * 1.0)/PageButtonCount)) * PageButtonCount);

    tmp = PageCount - PageButtonCount;
    if (tmp > 0 && tmp < startPage) { startPage = tmp; }

    if (CurrentPageIndex == 0) { s += lblPrev + " "; }
    else
    {
    j = CurrentPageIndex - startPage;
    if (startPage == 0) j -= 1;
    s += "<a href=\"" + ctrl + j + "', '')\">" + lblPrev + "</a> ";
    }

    if (startPage > 0) { s += "<a href=\"" + ctrl + "0','')\">...</a> "; }

    for (i = 0; i < PageButtonCount && (i + startPage) < PageCount; i ++)
    {
    tmp = startPage + i + 1;
    j = (startPage == 0) ? i : (i + 1);
    if (tmp == CurrentPageIndex + 1) { s += tmp + " "; }
    else { s += "<a href=\"" + ctrl + j + "','')\">" + tmp + "</a> "; }
    }
    if (startPage + PageButtonCount < PageCount - 1) {
    j = (startPage == 0) ? PageButtonCount : (PageButtonCount + 1);
    s += "<a href=\"" + ctrl + j + "','')\">...</a> ";
    }
    if (startPage + PageButtonCount < PageCount)
    {
    s += "<a href=\"" + ctrl_LastPage + "\">" + PageCount + "</a> ";
    }

    if (CurrentPageIndex >= PageCount - 1) { s += lblNext; }
    else
    {
    j = CurrentPageIndex - startPage + 1;
    if (startPage > 0) j += 1;
    s += "<a href=\"" + ctrl + j + "','')\">" + lblNext + "</a>";
    }

    return s;
    }

    Note that if don't want to use the page count link at the end then replace the red region with the following code:

    if (startPage + PageButtonCount < PageCount) {
    j = (startPage == 0) ? PageButtonCount : (PageButtonCount + 1);
    s += "<a href=\"" + ctrl + j + "','')\">...</a> ";
    }

    In .aspx page put:

    <asp:linkbutton id="btnFirstPage" onclick="DataGrid1_CustomPaging_First" Runat="server">First</asp:linkbutton>
    <asp:label id="lblPageNavBar" Runat="server"></asp:label>
    <asp:linkbutton id="btnLastPage" onclick="DataGrid1_CustomPaging_Last" Runat="server">Last</asp:linkbutton>


    Now set DataGrid1.PagerStyle.visible to False.
    Leave DataGrid1.AllowPaging as True, and DataGrid1.AllowCustomPaging as False.

    That's it! You are simulating datagrid's default paging with the freedom of customizing the style!

    --Appendix on 8/20/2009

    Now it's clear that this does not work in later versions of .NET. First the links' format is not DataGrid1$_ctl1$_ctl4, but like DataGrid1$ctl01$ctl04. More importantly, if set the visible property of the link buttons and datagrid pager to false, then __doPostBack won't work for these controls, as they are cleaned from the output. Therefore the above scheme works only for .NET 1.1 :(

    In later versions of .NET there is a pager template that can be used to format paging. Hope that's flexible enough.

    Another thought is that, the use of datagrid and dataview in later versions of .NET, are mostly for the ease of paging and sorting. Think carefully, I don't see anything else that datagrid and dataview can do special. If these can be handled from scratch, then there is no need to use these cumbersome controls. Actually I would prefer such a build-from-scratch approach, it avoids the burden of version incompatibility and allows the largest flexibility. Once the template is done, it can be used all the time without any more learning curve.

    Types of parameters in C#

    There are 4 types of parameter passing in C#:
    1) Value: pass by value.
    2) Out: like pointer in C/C++, allow return value. Don't have to be initialized first.
    3) Ref: like reference in C++, allow return value. Must be initialized first.
    4) Params: for variable length parameter list.

    More detailed explanation and examples:
    http://www.csharphelp.com/archives/archive225.html

    Blog Archive

    Followers