First-principles Analysis of Core Level Shifts