Thursday, April 25, 2013

Memory Segmentation of a Compiled Program

When a program is compiled and executed, it is loaded into the computer memory as binary codes.
Such binary codes of one program is structured in five segments, in the memory space. Each segment represents a special portion of the memory that is set side for a certain purpose.

Figure 1



The five segments of program memory are shown in figure 1. The first segment is called the Text segment, or Code segment. This is where the assembled machine language instructions of the program are located. Once started, the cpu will read and execute these instructions, one at a time. The register EIP always points to the next instruction to be executed. The execution of the instructions is not necessarily linear in the memory space, since the flow of execution is often redirected by control structures and functions.

The Data and Bss segments, are used to stored global and static variables. Specifically, the data segment is filled with initialized global and static variables, whereas the bss segment is filled with their  uninitialized  counterparts. Both data and bss segments are of fixed sizes.

The next segment down in the memory axis is the  Heap segment. It is a segment of memory that a programmer can directly control. Blocks of memory in this segment can be allocated and used. The size of the heap is dynamic, and it can be managed by allocator and deallocator algorithms. This means a programmer using the heap allocation functions can reserve and free memory on the fly. The growth of the heap moves downward toward higher memory addresses.

The last segment, Stack segment, is a very important a unique segment that deserves more explanation. In a modern program where a lots a functions are called by the main function, The flow of the instruction execution is managed by a mechanism call stack frame. When a function is called by the program, the program needs to remember a few things: the address of the next instruction to run after the function call is finished, the set of local variables associated with this function, the parameters passed to this function during this call. All of these information is stored collectively in the block of memory called stack frame. Each function call will create a stack frame in the stack segment. Figure 2 shows the content of one stack frame.

Figure 2


In the stack frame shown in Figure 2. Starting from the bottom of the stack, parameters passed to the functioned called are stored. The address of the instruction that the execution flow will return after the function call finish is stored above the parameters. The next slot up (lower address) is the so-called saved framed pointer, which is the address of the frame pointer of the previous stack frame. The address of this slot is stored in the EBP register, which always stores the frame pointer of the CURRENT stack frame. Above the saved frame pointer, the local variables are stored. The top of the current stack frame is always pointed by another register, ESP. If the function in question is calling another function, then the value in ESP will become the value of the frame pointer in the next stack frame. After the call of the function, the current stack frame will be popped out, and the previous stack frame then becomes the current stack frame.

Monday, April 15, 2013

US Imperialism and China

US imperial dreams, however, are hardly confined to setting the Middle East ablaze. Imperial ambitions—rooted in the capitalist logic of endless expansion—are inherently limitless. Thus, we see the US today readying to propel the greater Middle East into the abyss, while simultaneously “pivoting” to the Asia-Pacific in order to “contain” a rising China.

US imperialism, however, is destined for defeat (and sooner rather than later). The US, after all, can only use its immense military power to keep potential competitors in check for so long. The universal law of change cannot be held at bay by the barrel of a gun in perpetuity. As Lenin asked and answered in his pamphlet Imperialism: “Is it ‘conceivable’ that in ten or twenty years’ time the relative strength of the imperialist powers will have remained unchanged? Absolutely inconceivable.”

But imperial powers are always dangerously deluded by the strength of their power—impervious to its ultimate limits. As a George W. Bush administration official once remarked to the journalist Ron Suskind: “’We’re an empire now, and when we act, we create our own reality. And while you’re studying that reality—judiciously, as you will—we’ll act again, creating other new realities, which you can study too, and that’s how things will sort out. We’re history’s actors…and you, all of you, will be left to just study what we do.”
(One would be mistaken to believe that such hubris is not as present in the Obama White House as it was in the Bush administration.)

Such arrogance from the power elite—indicative of imperial rot—is but a byproduct of the imperialist imperative of endless expansion and conquest. And it is this very imperative that today compels US imperialism towards igniting a military conflagration in the Middle East threatening to ensnare the global powers. “A great cemetery,” as Luxemburg warned nearly a century ago, awaits such a triumph of barbarism."

A few months ago, James Baker remarked on the Charles Rose show that America is Greece if the dollar were not a reserve currency. Yet the US refuses to reduce Pentagon expenditure even though investment on infrastructure construction is much more productive. Obama's current strategy is that the US could pursue its war policies indefinitely as long as no Americans are killed. That is the reason why drones are extensively used all over the world. However, I firmly believe that this empire building policy is unsustainable and sooner or later the US will follow the Soviet Union as the next country that collapses economically due to military overspending.

Here is what Paul Craig Roberts (an editor of the Wall Street Journal and an Assistant Secretary of the U.S. Treasury) wrote recently:  "Why is Washington ramping up a new cold war?

The answer begins with President Eisenhower’s warning to the American people in his last public address about the military/industrial complex in 1962. I won’t quote the warning as it is available online. Eisenhower pointed out to Americans that unlike previous wars after which the US demilitarized, after World War II the cold war with the Soviet Union kept the power and profits flowing into the military/industrial complex, now known as the military/security complex. President Eisenhower said that the flow of power and profit into the military/industrial complex was a threat to the economic wellbeing and liberty of the American people.

No one paid any attention, and the military/security complex was glad to be rid of the five-star general war hero president when his second term expired."

"In other words, the problem with hot wars is that the need not to win them in order to keep them going (Korea, Vietnam, Iraq, Afghanistan are all long-term wars never won) in order that the profits and power continue to flow to the military/security complex demoralizes the US military and creates the world-wide impression that the “world’s sole superpower” cannot even defeat a few thousand insurgents armed with AK-47s, much less a real army.

In Iraq and Afghanistan more US soldiers have died from demoralization and suicides than from combat. In Iraq, the US was humiliated by having to end the war by putting the Sunni insurgents on the US military payroll and paying them to stop killing US troops. In Korea the US was stopped by an army of a backward third world country that lived on rice. What would happen today if the US “superpower’s” militarily confronted China, a country with an economy on which the US is dependent, about equal in size to the US economy, operating on its home territory? The only chance the evil in Washington would have would be nuclear war, which would mean the destruction of the entire world by Washington’s hubris.

Fortunately, profits are more important to Washington than ending life on earth. Therefore, war with China will be avoided, just as it was avoided with the Soviet Union.

However, China will be presented by Washington and its prostitute media, especially the New York Times, Washington Post, and Murdoch’s collection of whores, as the rising threat to America. The media story will shift the importance of America’s allies from Europe to countries bordering the South China Sea. American taxpayers’ money, or newly printed money, will flow into the “new alliance against China.”

China’s rise is a great boon to the US military/security complex, which governs america in which there is a pretense of “freedom and democracy.” China is the profitable replacement for the “Soviet threat.” As the days go by, the prostitute media will create in the feeble minds of Americans “The CHINA Threat.”

Soon whatever little remains of the US living standard will be sacrificed to Washington’s confrontation with China, along with the seizure of our pensions and personal savings in order to deter “the China threat.”
If only Americans were an intelligent people. Then they might have some prospect of holding on to their incomes, remaining wealth, and liberty. Unfortunately, Americans are so thoroughly plugged into the Matrix that they present as a doomed people, incapable of thought, reason, or ability to comprehend the facts that the rest of the world sees clearly."

All this hype about Iran, China, etc. is all designed to ensure that the Pentagon does not get hit by budget cuts!!!

Sunday, April 14, 2013

GNU Developer Tools

Here are a few debugging tools from the GNU developer toolkit. They are commonly available in most of the Linux distribution. My Linux box runs Ubuntu 12.10.

1. objdump: display hexadecimal and assembly representations of machine languages of an executable.

typical usage: objdump -D 'name-of-the-executable'

You need to understand assembly language to be able to tell what's in output.

2. gdb: debugger to allow programmer to step through a compiled program and examine program memory and processor registers.

A GDB session can be launched as followed:

gdb -q 'name-of-the-executable'

Then one can interactively enter commands to control the start, pause, quit the program, or set breakpoints and examine register values. Common commands used in gdb are:

-break 'function_name': set breakpoint at the entry of a function
-run: start the program (until breakpoint)
-disassemble function: disassemble a function into assembly language
-info registers: see the values in the registers
-info register name-of-register: see the values stored in a particular register
-x/Nxw $name-of-register: examine the content of the memory, pointed by the register. N indicates the number of words to after that starting address.
-x/i: examine the assembly instruction pointed by an address.
-x/s: examine the string pointed by an address.
-continue: continue (until the next breakpoint or the end of the program)
-quit: quit the program

3. Hexdump: to display the hexadecimal content of a file. Common options:

- hexdump -C filename: to display the content of the file in hex, one byte at a time, followed by character

- hexdump -x filename: to display the content of the file in hex, two bytes at a time.